简体   繁体   中英

How to create JSONP callback?

I have JSON file generated on my server , but I want to access that data from other host . What should I do on my server , or JSON file to make that data accessible from other domains like JSONP ?

Assuming it's exposed by some web-accessible method, you need to accept a callback (or similar) parameter which then just becomes a wrapper to the JSON data. eg

If you had:

/some/service.json

Which returned:

{"this":"is","JSON":"data"}

You then allow the service to be passed a callback :

/some/service.json?callback=foo

Which in turn results in:

foo({"this":"is","JSON":"data"})

That's all there really is to making the response adhere with JSONP.

i think this below code help you

$.ajax({
    type: "POST",
    url: "xyz.com",
    data: jsondata,
    dataType: "jsonp",
    success: function(data) {

        if(data.flag == true){

            alert(data.msg);


        } else {
            alert("not sucess");

        }
    }

    }); 

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