2011-06-02 77 views
6

更新1:通过jQuery获取JSONP

这是我所得到的在浏览器中,如果我键入

http://www.remote_host.com/feed.php?callback=jsonpCallBack

{ 
    "rss": { 
     "channels": [ 
      { 
       "title": "title goes here", 
       "link": "http://www.remote_server.com/feed.php", 
       "description": "description goes here", 
       "items": [ 
        { 
         "title": "item title goes here", 
         "link": "item link goes here", 
         "pubDate": "item date goes here", 
         "description": "item description goes here" 
        }, 
        { 
         "title": "item title goes here", 
         "link": "item link goes here", 
         "pubDate": "item date goes here", 
         "description": "item description goes here" 
        }, 
        { 
         "title": "item title goes here", 
         "link": "item link goes here", 
         "pubDate": "item date goes here", 
         "description": "item description goes here" 
        } 
       ] 
      } 
     ] 
    } 
} 

所以这不是JSONP?

原题:

我有以下脚本,我试图从远程主机获得JSON数据:

$(document).ready(function() { 
    get_json_feed(); 

    function get_json_feed() { 
     $.ajax({ 
      url: 'http://www.remote_host.com/feed.php?type=json', 
      type: 'GET', 
      dataType: 'jsonp', 
      error: function(xhr, status, error) { 
       alert("error"); 
      }, 
      success: function(json) { 
       alert("success"); 
      } 
     }); 
    } 
}); 

但由于某些原因,我得到一个错误和警告:

Warning: Resource interpreted as Script but transferred with MIME type text/html.

Error: Uncaught SyntaxError: Unexpected token :

我在做什么错?

+1

没有ü尝试改变类型? – diEcho 2011-06-02 11:23:32

+0

@diEcho,哪种类型?我在客户端类型和数据类型有两种类型,我在服务器端有一个类型。 – oshirowanen 2011-06-02 11:24:51

+1

这听起来像服务器不返回JSONP。你应该尝试(a)'feed.php?type = jsonp'和(b)如果服务器支持JSONP,它通常会接受一个参数来指定回调名称,如:'feed.php?type = jsonp&callback = ?'。您必须阅读您正在使用的服务的文档。 – 2011-06-02 11:24:55

回答

7

的JSONP“协议”所依赖的现场答复与形式的JavaScript语句您的要求上,

someFunction(someJSON) 

函数的名字从你的代码参数提供,用意念之中响应脚本一旦被浏览器使用并解释,就会导致对该函数的调用,并带有一个JSON解析blob,即一个JavaScript对象。 jQuery库将为你做一些bookeeping工作,甚至可以创建全局范围的函数来调用(这将是代码,只是调用你提供的作为“成功”参数的回调)。

因此,您应该检查来自该服务器的实际响应是什么样子。这听起来好像它可能不是一个准备好以这种方式回应的服务器。您可能需要确保您的网址上有一个额外的参数,形式为“callback =?”。

+0

请参阅更新1以确认。 – oshirowanen 2011-06-02 11:52:06

+0

对 - 这只是普通的JSON。 JSONP协议要求将JSON封装在函数调用中。浏览器通过创建'

3

看起来像服务器返回错误的内容类型标头。

+1

我已经添加了一个内容类型头'header(“content-type:text/javascript”);',所以警告现在不见了。 – oshirowanen 2011-06-02 11:52:50

6

我不知道你所面临的到底是什么错误,但也有一些有用的提示使用jsonphere

  • error:此处理程序不叫跨域脚本和JSONP请求。
  • 在ajax参数中写入jsonp: 'callback', jsonpCallback: 'jsonpCallback'。 设置jsonp回调,然后设置jsonpCallback到jsonpCallback使得查询字符串是这样的:使用JSONP的JSON块

    http://domain.com/jsonp-demo.php?callback=jsonpCallback&name=watever

  • 负荷。将在您的网址末尾添加额外的?callback=?以指定回叫。

你完整的脚本应该是这样的:

<script> 
    $(document).ready(function(){ 

     $("#useJSONP").click(function(){ 
      $.ajax({ 
       url: 'http://domain.com/jsonp-demo.php', 
       data: {name: 'Chad'}, 
       dataType: 'jsonp', 
       jsonp: 'callback', 
       jsonpCallback: 'jsonpCallback', 
       success: function(){ 
        alert("success"); 
       } 
      }); 
     }); 

    }); 

    function jsonpCallback(data){ 
     $('#jsonpResult').text(data.message); 
    } 
    </script> 

Example here