0

I receive the error POST https://thewebsite.com 400 (Bad Request) when using $.post that way:

$.post("https://website.com/blabla",
{
  domain: "infoinfo.com",
  room: "someInfo",
  application: "someInfo",
  ident: "someInfo",
},
function (data,status) {
  alert("Data: " + data + "\nStatus: " + status);
});

I tried setting res.header('Access-Control-Allow-Origin', "*"); in my route but that didn't work.

Any ideas on how I could fix this?

Ps.: The server I am posting to is service website (xirsys.com), I am pretty sure they allow external domains already. I'll contact them during the day if I can't find a solution (I am using the jQuery post as they suggested :/

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Dan P.
  • 1,707
  • 4
  • 29
  • 57

4 Answers4

0

This is due to the Same-origin policy. All the browsers as a security measure would not allow any cross domain requests.

http://en.wikipedia.org/wiki/Same-origin_policy

Like in your case you are posting to thewebsite.com domain from a different domain. A work around is to use the jsonp (the server should support json padding) from the jquery.

Check these sites for more info

http://www.jquery4u.com/json/jsonp-examples/
http://en.wikipedia.org/wiki/JSONP
http://stackoverflow.com/questions/6871021/how-to-enable-cross-domain-request-on-the-server
rozar
  • 1,058
  • 3
  • 15
  • 28
0

Try to add this to your AJAX call:

contentType: "application/json; charset=utf-8",
dataType: "json"

Another reason maybe because of the same origin policy:

In computing, the same origin policy is an important security concept for a number of browser-side programming languages, such as JavaScript. The policy permits scripts running on pages originating from the same site to access each other's methods and properties with no specific restrictions, but prevents access to most methods and properties across pages on different sites.

You can find out more informations about this issue from MDN docs or doing some research on Google about this topic.


You can try to use $.axax() like this:

$.ajax({
    type: 'POST',
    url: "https://website.com/blabla",
    data: {
        domain: "infoinfo.com",
        room: "someInfo",
        application: "someInfo",
        ident: "someInfo"
    },
    dataType: "json",
    contentType: "application/json",
    success: function (e) {
        alert("Data: " + data + "\nStatus: " + status);
    }

});
Felix
  • 37,892
  • 8
  • 43
  • 55
  • The post is actually to https://beta.xirsys.com/getIceServers -- I'm pretty sure they allow external domains already. How would you go about adding the contentType/dataType into my example? – Dan P. Feb 24 '14 at 06:14
  • Actually my thing didn't work, it only got the default json from the site. Trying yours. – Dan P. Feb 24 '14 at 06:22
  • @DanyP.. Accept the useful answer or post your solution and accept it as the answer. Thanks – rozar Feb 24 '14 at 06:56
0

SAMPLE REQUEST:

$.ajax({
            url: "http://yoururl",
            type: "POST",
            crossDomain: true,
            data: JSON.stringify(somejson),
            dataType: "json",
            success: function (response) {
                alert("success");
            },
            error: function (xhr, status) {
                alert("error");
            }
        });

SAMPLE RESPONSE IN PYTHON:

response = HttpResponse(json.dumps('{"status" : "success"}'))
response.__setitem__("Content-type", "application/json")
response.__setitem__("Access-Control-Allow-Origin", "*")

return response
Hassan Zaheer
  • 1,361
  • 2
  • 20
  • 34
0

The link I was posting to was actually not good. The service I am using updated the link in their API to reflect the right one.

Dan P.
  • 1,707
  • 4
  • 29
  • 57