Sending JSONP vs. JSON data?

后端 未结 2 1436
清歌不尽
清歌不尽 2020-12-03 11:56

I am making a web service that needs to return data in JSONP format. I am using the JSON taglib for JSP, and I thought all that had to be added were parenthesis, but I canno

相关标签:
2条回答
  • 2020-12-03 12:16

    I added one example to address Cross Domain JSONP ( Json with padding ) with Jquery and Servlet or JAX-WS webservice.

    Please check this article.
    http://reddymails.blogspot.com/2012/05/solving-cross-domain-problem-using.html

    0 讨论(0)
  • 2020-12-03 12:43

    JSONP is simply a hack to allow web apps to retrieve data across domains. It could be said that it violates the Same Origin Policy (SOP). The way it works is by using Javascript to insert a "script" element into your page. Therefore, you need a callback function. If you didn't have one, your Javascript would have no way to access the JSON object. But by using JSONP, your Javascript code can call the callback function.

    So you must specify the callback name. So your function might look like this:

    private static String getJSONPObject(String callback, String s) throws JSONException {
        return callback + "(" + new JSONObject(s) + ")";
    }
    
    0 讨论(0)
提交回复
热议问题