来自 jQuery get 的 JSON 响应引发“无效标签”

发布于 2024-09-09 04:02:20 字数 975 浏览 2 评论 0原文

       $.ajax({
  beforeSend: function(xhr) {
 xhr.setRequestHeader('Authorization', "Basic YWRtaW46YWRtaW4=");        
},
   url: "https://test.com/incident.do?JSON&callback=?&sysparm_action=getRecords",
   dataType: "json",
   contentType: "application/json",
   method: 'GET',
   success: function(a,b,c) { 
    alert(a);
         }          
 });

我通过按下按钮来调用此函数...Firebug 显示我收到此 JSON 响应(我知道该响应是有效的)[为清楚起见而截断]

{
    "records": [
        {
            "service_offering": "",
            "number": "INC0000009"
        },
        {
            "service_offering": "",
            "number": "INC0000010"
        }
]
}

Firebug 显示错误“无效标签” https://test.com/incident.do?JSON&callback= jsonp1279049933243&sysparm_action=getRecords 第 1 行

我该如何解决这个问题? 谢谢!

       $.ajax({
  beforeSend: function(xhr) {
 xhr.setRequestHeader('Authorization', "Basic YWRtaW46YWRtaW4=");        
},
   url: "https://test.com/incident.do?JSON&callback=?&sysparm_action=getRecords",
   dataType: "json",
   contentType: "application/json",
   method: 'GET',
   success: function(a,b,c) { 
    alert(a);
         }          
 });

I call this function with a button press... Firebug shows that I get this JSON response (which I know is valid) [truncated for clarity]

{
    "records": [
        {
            "service_offering": "",
            "number": "INC0000009"
        },
        {
            "service_offering": "",
            "number": "INC0000010"
        }
]
}

Firebug shows the error "invalid label
https://test.com/incident.do?JSON&callback=jsonp1279049933243&sysparm_action=getRecords
Line 1

How can I fix this?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

撑一把青伞 2024-09-16 04:02:20

您的响应是 JSON,这是有效的,但这不是 jQuery 想要的。当您在 URL 中指定 &callback=? 时,jQuery 期待 JSONP 响应,该响应看起来有所不同,您的响应应该

jsonp1279049933243({
  "records": [
    {
        "service_offering": "",
        "number": "INC0000009"
    },
    {
        "service_offering": "",
        "number": "INC0000010"
    }
  ]
});

当您指定 callback 时会发生什么=? 是 jQuery 为您的 success 函数生成一个名称,在本例中是 jsonp1279049933243,JSONP 的工作原理是生成一个

<script type="text/javascript">
  jsonp1279049933243({ "records": [....] });
</script>

现在有效发生的是:

<script type="text/javascript">
  { "records": [....] }
</script>

...这不是有效的 JavaScript。现在当然是通过 src=https://test.com/incident.do?JSON&callback=jsonp1279049933243&sysparm_action=getRecords 加载,但无效语法/标签错误是相同的。

Your response is JSON, which is valid, but that's not what jQuery's looking for. When you specify &callback=? in the URL, jQuery is expecting a JSONP response, which looks different, your response should be

jsonp1279049933243({
  "records": [
    {
        "service_offering": "",
        "number": "INC0000009"
    },
    {
        "service_offering": "",
        "number": "INC0000010"
    }
  ]
});

What happens when you specify callback=? is that jQuery generates a name for your success function, in this case jsonp1279049933243, JSONP works by just generating a <script> tag in the page, so when it fetches that url, it's really just including a JavaScript file, running a function, but instead of this:

<script type="text/javascript">
  jsonp1279049933243({ "records": [....] });
</script>

What's effectively happening now is:

<script type="text/javascript">
  { "records": [....] }
</script>

...which isn't valid JavaScript. Now of course it's loaded via src=https://test.com/incident.do?JSON&callback=jsonp1279049933243&sysparm_action=getRecords, but the invalid syntax/label error is the same.

迟月 2024-09-16 04:02:20

您可能想验证该服务是否支持 jsonp。如果没有,您需要设置服务器端代理。

使用 jQuery 获取 json 数据返回无效标签错误

You may want to verify that the service supports jsonp. If not you'll need to set up a server side proxy.

Using jQuery to get json data returns invalid label error

孤星 2024-09-16 04:02:20

你说你知道它是有效的。你能将它导入到 parseJSON 函数中,看看你是否幸运吗?

$.parseJSON(str_json);

注意:您没有提到您正在使用哪个版本的 jQuery。该方法仅在 jQuery 1.4.1 及以上版本中可用。

You say that you know its valid. Can you funnel it into the parseJSON function and see if you have any luck with that?

$.parseJSON(str_json);

Note: You haven't mentioned which version of jQuery you're using. This method is available only in jQuery 1.4.1 and above.

时光匆匆的小流年 2024-09-16 04:02:20

您确定 json 有效吗?检查其是否在 JSONLint 处验证。

Are you sure the json is valid? Check it validates at JSONLint.

情场扛把子 2024-09-16 04:02:20

尝试以纯文本形式获取数据并在客户端将其转换为 json。我使用“http://www.json.org/json2.js”脚本来执行这。它帮助我解决了应用程序中的“无效标签”问题。 (我提到这个脚本是因为 eval() 函数没有帮助,不知道为什么......)

祝你好运:)

try to get the data as a plain text and convert it into json on client side. I use "http://www.json.org/json2.js" script to do this. It helped me to resolve "invalid label" problem in my application. (I mentioned the script because eval() function didn't help, no idea why...)

good luck :)

小草泠泠 2024-09-16 04:02:20

如果您使用的是 1.5.2(也可能是旧版本),如果您不需要或想要它,可以禁用 jsonp。

因此,对于所有 ajax 调用,请在页面上的某处添加以下内容:

$.ajaxSetup({
    jsonp: null,
    jsonpCallback: null
});

或者,对于一次调用,请添加以下内容:

jsonp: null,
jsonpCallback: null,

因此,在您的示例中:

$.ajax({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('Authorization', "Basic YWRtaW46YWRtaW4=");        
    },
    url: "https://test.com/incident.do?JSON&callback=?&sysparm_action=getRecords",
    dataType: "json",
    contentType: "application/json",
    method: 'GET',
    jsonp: null,
    jsonpCallback: null,
    success: function(a,b,c) { 
        alert(a);
    }          
});

If you are using 1.5.2 (and maybe older versions), you can disable jsonp if you do not need it or want it.

So for all ajax calls add this somewhere on your page:

$.ajaxSetup({
    jsonp: null,
    jsonpCallback: null
});

Or for just one call, add this:

jsonp: null,
jsonpCallback: null,

So in your example:

$.ajax({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('Authorization', "Basic YWRtaW46YWRtaW4=");        
    },
    url: "https://test.com/incident.do?JSON&callback=?&sysparm_action=getRecords",
    dataType: "json",
    contentType: "application/json",
    method: 'GET',
    jsonp: null,
    jsonpCallback: null,
    success: function(a,b,c) { 
        alert(a);
    }          
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文