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.

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

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败