简体   繁体   中英

Error in CORS ajax request

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:

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

DEMO

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM