Still confused using cross-domain XMLHTTPRequest

I need to send POST data to a server in another domain. This server uses SSL and expects the data to be in the form of a JSON string. I am trying to do this from javascript.

I am creating data and using JSON.stringify () to get it in the correct format. Then I send it as follows:

var url = "https://api.postageapp.com/v.1.0/send_message.json"; http=new XMLHttpRequest(); http.open("POST", url, true); http.setRequestHeader("Content-type", "application/json"); http.setRequestHeader("Connection", "close"); // create the data in a data structure named post_data var JSONText = JSON.stringify(post_data); http.send(JSONText); 

Perform packet tracing. I see that my client is shaking hands with the server, but then the server twice responds with an โ€œEncrypted alertโ€, including the last time it sends a packet back. The browser debugger always shows error 405 - Method Now Allowed.

What am I missing to get this to work? When they try it in their domain, it works fine.

+4
source share
3 answers

You need the server to return the HTTP header as follows:

 header('Access-Control-Allow-Origin: *'); 

Live example: Making JavaScript requests for cross-domains using XMLHttpRequest or XDomainRequest

+1
source

You cannot cross-domain a message like this.

An alternative is to use a server proxy (read this link to get a good explanation of why you cannot do this) or an iframe approach .

0
source

Strictly speaking, this should not be possible (due to security issues), however, using the JSONP workaround, you can achieve this using the RESTful web service.

See the link below. http://en.wikipedia.org/wiki/JSONP

MS has some code that you can download somewhere on the Internet with specific associations that the code invokes.

  • JSONPBehaviour.cs
  • JSONPBindingElement.cs
  • JSONPBindingExtension.cs
  • JSONPEncoderFactory.cs
0
source