Returning JSONP instead of JSON from a JSP

前端 未结 3 796
梦毁少年i
梦毁少年i 2021-01-22 20:06

I found this question on setting the response type to json from a jsp but I\'m in need of setting the response type to jsonp for cross-domain access. Would it still be this:

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-22 20:48

    I recently had to do this. In the server side I had something like so:

    string callbackName = queryMap['callback']; //jquery will pass in some name in our .getJSON call below
    string jsonData = getJsonData();
    string jsonp = callbackName + "(" + jsonData + ")";
    
    response.SetContentType('application/javascript');
    response.Send( jsonp );
    

    And in the javascript it was something like so:

    var url = getUrl() + "?callback=?";
    $.getJSON(url,function(onSuccessData){ alert(onSuccessData); });
    

提交回复
热议问题