【问题标题】:Parse JsonP error解析 JsonP 错误
【发布时间】:2014-04-09 18:01:30
【问题描述】:

我正在尝试解析一个 Json,它是 here。当我启动浏览器时,2 秒后,出现错误:

资源被解释为脚本,但以 MIME 类型 text/html 传输:

为什么?这是什么错误?

JS

function example()
{   
  alert("hello nice");
  var URL = "http://sath3g.altervista.org/index.php";
  $.ajax(URL, {
        crossDomain: true,
        dataType: "jsonP",
        type: 'GET',
        success: function (data, text, xhqr) {
            $.each(data, function(i, item) {
                alert(item);
                });
               },
        });
 }

HTML

<!DOCTYPE html>
<html>
<head>
  <title>Parse Json</title>
  <script src="parse.js"></script>
  <script type="text/javascript" src="jquery-2.1.0.min.js"></script>
</head>
<body onload="example()">
   <header id="title">
        Parsami tutta  
   </header>
   <form>

    </form>
</body>
</html>

【问题讨论】:

标签: javascript jquery json parsing jsonp


【解决方案1】:

您没有犯错误(除非您也是该网络服务的作者)。 sath3g.altervista.org 正在为其 JSON 和 JSONP 文档发送错误的 Content-Type HTTP 响应标头。

看起来它是由了解一些 PHP 但对 HTTP 不够了解的人编写的。默认情况下,PHP 会声称它正在输出 HTML,如果您发送的是 JSON (header("Content-Type: application/json")) 或 JSONP (header("Content-Type: application/javascript")),您需要明确覆盖它。

【讨论】:

  • 我在哪里插入 (header("Content-Type: application/javascript"))??在我的 Php 中?
  • 在输出任何内容之前。
  • 我插入php文件,但问题是一样的
  • 应该可以解决它。可能是你在 header 之前输出了内容,或者你把它放在了错误的 PHP 文件中。
【解决方案2】:

你需要修改你的ajax调用属性

dataType: 'jsonp',

这里的jsonp是小写的

jsonp: 'jsonp',

添加上述属性后也可以尝试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 2012-03-16
    相关资源
    最近更新 更多