user246114
user246114

Reputation: 51631

javascript call url from different domain

I want to post some data via javascript to another domain. Something like:

http://www.othersite.com/submitfunnyname?name=blah

The other site (othersite.com) has a REST interface that you can call (well actually this is a get example) to submit a funny name to them.

Can I do this already with javascript? I'm a little confused on this - I know if that service wants to return some data, I'd need to use something like JSON-P - even though here I'm submitting some data, I guess the service will return some message structure letting me know the result, so it would have to be JSON-P, right?

Thanks

Upvotes: 2

Views: 1044

Answers (2)

mbmcavoy
mbmcavoy

Reputation: 2686

Not a particular expert in JavaScript, but isn't this an example of "cross-site scripting", which is not allowed due to possible security threats?

I believe you need to have all HTTP calls being made to the same server domain as the page. You could have a handler on your own site pass the information on to the othersite.com.

Upvotes: 2

Justin Ethier
Justin Ethier

Reputation: 134177

You can either use JSON-P if the site supports it, or you can use your web server as a proxy - by making requests to your server, which will in turn use a library such as cURL to make the actual request to the remote site.

Upvotes: 1

Related Questions