douqian1517 2014-01-10 22:47
浏览 47
已采纳

Objective-C:来自jquery回调的json响应

I'm building and iOS application and trying to get the json data from a url similar to:

example.com/ajax/u.php?callback=jQuery84054761566_1389381628746

Is anyone familiar with these types of callbacks?

Typical HTTP requests in obj-c return 200 status and a null response object with this url.

  • 写回答

1条回答 默认 最新

  • doufei2662 2014-01-11 06:21
    关注

    That's a JSONP callback used for...well, JSONP. JSONP is used on webpages to avoid the 'same origin' restriction. That is, a webpage loaded from a certain domain (say abc.com) cannot call/access resources on a separate domain (say xyz.com).

    The way they (web front-end developers) get around this is by using the <script> tag which is exempt from this restriction. However the return of a <script> tag would just be parsed - to cause it to execute something, you tell it a function name that exists in your JavaScript code, in this case, jQuery84054761566_1389381628746.

    So instead of returning a regular JavaScript object, say

    {key : 'value'}
    

    the server then returns a function invocation

    jQuery84054761566_1389381628746({key : 'value'}) 
    

    and you have, in your webpage JavaScript, the function definition for jQuery84054761566_1389381628746 that does something:

    jQuery84054761566_1389381628746 = function(data){
      alert(data.key);
    };
    

    iOS does not have this same-origin/cross-site restriction so you don't need JSONP - you can call whatever server you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码