1

I'm making requests from a page served on localhost to a server I have hosted on a VPS. I had issues making GET requests and found that I had to enable CORS on the server, which worked. Now I'm trying to making POST requests, but I'm running into the same issue I did with the GETs. I get back

{'readyState': 0, 'status': 0, "statusText": "error"}

The request i'm sending:

var story = $("#post_story").val();
var title = $("#title").val();

var url = BASE_URL + "/stories";

var data = {
    content: story,
    title: title,
    category: "some_cat"
};

$.ajax({
    url: url,
    crossDomain: true,
    type: "POST",
    dataType: "json",
    contentType: "application/json",
    data: data,
    success: (function (data, status) {
        console.log(data);
        $("#post_story").text("");
        $("#title").text("");
    }),
    error: (function (xhr, status, err) {
        console.log(status);
        console.log(error);
        console.log(xhr);
    })
});

I'm using tshark on the server and it doesn't show anything incoming.

If I change json to jsonp, tshark shows:

GET /stories?callback=jQuery1102020064887526511765_1385831485614&content=somewords&title=sometitle&category=some_cat&_=1385831485615 HTTP/1.1 

but on the client I get a 500 Internal server error

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Chris
  • 7,270
  • 19
  • 66
  • 110
  • 1
    JSONP only supports GET as it inserts a script file into the DOM, but a regular JSON request to a CORS enabled server should work with POST, if the server is listening and answering POST requests, and sends the proper headers. – adeneo Nov 30 '13 at 17:16
  • 1
    I'd suggest you get the 'Postman' app for Google Chrome, which allows you to simulate requests and debug this sort of thing in no time at all. – jacktheripper Nov 30 '13 at 17:17
  • @jacktheripper +1 for the resource, just tried it out. And it comes back 201! So now I'm more confused, cause the server is clearly working. – Chris Nov 30 '13 at 17:20
  • Can you have a look at the 'Network Request' panel in the Chrome dev-tools, and compare that to the request you made via Postman? Might be revealing... – jacktheripper Nov 30 '13 at 17:24
  • @jacktheripper The method in the dev tools shows `OPTIONS` instead of `POST`. – Chris Nov 30 '13 at 17:28
  • @Chris something a lot of people don't realize with cross domain. JSON output, no CORS for example will return json, and 200 but browser won't use it and won't complete – charlietfl Nov 30 '13 at 17:28
  • **I wrote an answer related to this question here: [Loading cross domain html page with jQuery AJAX](http://stackoverflow.com/questions/15005500/loading-cross-domain-html-page-with-jquery-ajax/17299796#17299796)** – _the last one, supports https_ – jherax Jun 26 '14 at 17:15

0 Answers0