0

In php i do echo json_encode($dump);

If echo it out using php i get {"load":"0.64 0.58 0.52 2\/361 12978\n","procs":"8\n"}

Than i make CORS Request using dataType:jsonp

$(function () {
    $.ajax({
        type: "POST",
        ContentType: 'application/json; charset=utf-8',
        url: 'http://labs.isaumya.com/loadtest/load',
        dataType: "jsonp",
        success: function (response) {
            console.log(response.data);
        },
        error: function (xhr, status, error) {
            console.log(error);
        }
    });
});

I get this error on the console:

https://i.stack.imgur.com/xAxx6.png

DEMO

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64

1 Answers1

3

You are dealing with JSON, not JSONP. dataType: "jsonp", should be dataType: "json",

You can remove the data parameter entirely if your server outputs the correct content-type header for JSON (application/json).


JSONP is a hack to work around the Same Origin Policy from before CORS was designed and implemented by browsers. CORS is the modern approach to making cross origin requests. You use it instead of JSONP.


Both CORS and JSONP are technologies that must be supported by the server. http://labs.isaumya.com/loadtest/load doesn't appear to support either. You will have to modify the server if you want it to supply data in JSONP format or grant permission with CORS.


Unrelated to your actual problem:

You have no data parameter so you aren't sending JSON to to the server, remove the ContentType parameter. Since you aren't POSTing any data, you should probably be making a GET request, not a POST request.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • if i use `json` than it will not serve `CORS` Request . – user3527203 Apr 12 '14 at 16:35
  • @user3527203 — CORS is the modern alternative to JSONP. You don't use JSONP for a CORS request. – Quentin Apr 12 '14 at 16:36
  • see --> http://jsfiddle.net/w7D5V/2/ i made changes you say but still getting error `XMLHttpRequest cannot load http://labs.isaumya.com/loadtest/load. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access. (index):1` – user3527203 Apr 12 '14 at 16:40
  • As mentioned in the answer: "You will have to modify the server if you want it to supply data in JSONP format or grant permission with CORS" – Quentin Apr 12 '14 at 16:40
  • how to modify server for jsonp request ? – user3527203 Apr 12 '14 at 16:42
  • 1
    See [this question](http://stackoverflow.com/questions/3295692/does-jsonp-require-server-modifications) – Quentin Apr 12 '14 at 16:43
  • @user3527203 You need to modify the header – Rahil Wazir Apr 12 '14 at 16:44
  • @RahilWazir — That's for CORS, not for JSONP. – Quentin Apr 12 '14 at 16:44
  • @Quentin is correct -- @user3527203 -- CORS requests are not the same as jsonp. To allow JSONP, you must configure your server to pad json results with a/the function name passed via your query string. `JSONP === padded json` – J.Wells Apr 15 '14 at 02:22