$.ajaxSetup() 中的 jquery 解析器错误

发布于 2024-11-07 01:04:22 字数 1117 浏览 0 评论 0原文

我目前正在jquery 中开发一个程序。我的程序在 Firefox 3.5 中运行良好,但直到我将浏览器升级到 Firefox 4.0 后才运行正常。从那时起,“解析器错误”就再也没有出现过,这让我很头疼。

我注意到这是我的代码中第一个“parsererror”显示的部分:

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

   $.ajaxSetup({
      timeout: 10000,
      error: function(xhr, msg, e) {
        showMessage('HTTP error: ' + JSON.stringify(msg) + '.'); //this is the parsererror
      }    
   });  
   .
   .
})  

不仅如此,我的动态选项卡不再出现在我的页面中。我注意到,每次删除此行 '',看起来但我的其他 jquery 元素不好。我不知道出了什么问题。也许是某种不兼容问题,但我只是不知道从哪里开始修复。请帮忙。

编辑: 这是它返回的 json。据推测,这适用于我的动态菜单,它将创建您单击的每个选项卡菜单。但这并没有显示出来。

 [ 
      {"title": "File","submenus":[
         {"title": "Open","submenus":[]},         
         { "title": "New", "submenus":[]},
         { "title": "Save as", "submenus":[]},
         { "title": "Save", "submenus":[]}
      ]},
      { "title": "View","submenus":[]},
      { "title": "viewAll", "submenus":[]},
      { "title": "Close","submenus":[]},
      {"title":"jQgrid", "submenus":[]}  
 ]

i am currently working on a program in jquery. My program works fine in firefox 3.5 but not until i upgraded my browser to firefox 4.0. Since then, the 'parsererror' never failed to show and it gives me a bad headache.

I've notice that this is the part of my code that FIRST 'parsererror' shows:

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

   $.ajaxSetup({
      timeout: 10000,
      error: function(xhr, msg, e) {
        showMessage('HTTP error: ' + JSON.stringify(msg) + '.'); //this is the parsererror
      }    
   });  
   .
   .
})  

And not only that, my dynamic tab no longer appear in my page. I notice that everytime if remove this line '<script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>', it appears but my other jquery element is no good. I don't know what's wrong. Maybe it some sort of incompatibility issues, but i just dont where to start fixing. Please help.

EDIT:
this is the json it returned. This is supposedly for my dynamic menu that will create tab Evry menu you clicked. But this doesn't show.

 [ 
      {"title": "File","submenus":[
         {"title": "Open","submenus":[]},         
         { "title": "New", "submenus":[]},
         { "title": "Save as", "submenus":[]},
         { "title": "Save", "submenus":[]}
      ]},
      { "title": "View","submenus":[]},
      { "title": "viewAll", "submenus":[]},
      { "title": "Close","submenus":[]},
      {"title":"jQgrid", "submenus":[]}  
 ]

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

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

发布评论

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

评论(2

谁与争疯 2024-11-14 01:04:22

jQuery Ajax 错误文档的描述

错误(jqXHR,textStatus,errorThrown)

功能

一个函数
如果请求失败则调用。这
函数接收三个参数:
jqXHR(在 jQuery 1.4.x 中,
XMLHttpRequest) 对象,一个字符串
描述错误的类型
发生并且可选的异常
对象,如果发生的话。可能的
第二个参数的值
(除了空)是“超时”,“错误”,
“中止”和“解析器错误”。当
HTTP 发生错误,errorThrown
接收文本部分
HTTP 状态,例如“未找到”或
“内部服务器错误。”从 jQuery 开始
1.5、错误设置可以接受函数数组。每个函数都会
依次被调用。注意:该处理程序
不调用跨域脚本
和 JSONP 请求。这是一个阿贾克斯
活动

在您的代码中,您可以

JSON.stringify(msg)

查看 jQuery 文档,您将看到第二个参数是一个字符串,而不是您期望的 JSON 对象。解析器正在查看字符串并抛出您所看到的解析错误。

现在,如果发生错误并且对象表示存在错误。 JSON 要求名称用双引号引起来。所以人们认为:

{
  foo : "bar",
  color : "red",
  num : 1
} 

是有效的 JSON,但事实并非如此。以下内容有效。

{
  "foo" : "bar",
  "color" : "red",
  "num" : 1
} 

Description from jQuery Ajax's error documentation

error(jqXHR, textStatus, errorThrown)

Function

A function to be
called if the request fails. The
function receives three arguments: The
jqXHR (in jQuery 1.4.x,
XMLHttpRequest) object, a string
describing the type of error that
occurred and an optional exception
object, if one occurred. Possible
values for the second argument
(besides null) are "timeout", "error",
"abort", and "parsererror". When an
HTTP error occurs, errorThrown
receives the textual portion of the
HTTP status, such as "Not Found" or
"Internal Server Error." As of jQuery
1.5, the error setting can accept an array of functions. Each function will
be called in turn. Note: This handler
is not called for cross-domain script
and JSONP requests. This is an Ajax
Event

In your code you have

JSON.stringify(msg)

Looking at the jQuery docs, you will see that the second argument is a string and not a JSON object like you are expecting it to be. The parser is seeing the string and throws the parse error that you are seeing.

Now if an error is occurring and the object is saying that there is an error. JSON requires that the name has double quotes around it. So people think:

{
  foo : "bar",
  color : "red",
  num : 1
} 

is valid JSON, but it is not. The following is valid.

{
  "foo" : "bar",
  "color" : "red",
  "num" : 1
} 
眼眸 2024-11-14 01:04:22

听起来上面的代码运行得很好 - 它告诉您 XMLHttpRequest 返回了一个带有消息 “parsererror” 的错误。您可能可以通过检查错误函数中的 e 变量来了解更多信息。但您提供的代码不会导致错误,因此我们无法对其进行调试。

It sounds like the code above works perfectly - it's telling you that the XMLHttpRequest returned an error with the message "parsererror". You could probably learn more about it by inspecting the e variable in your error function. But the code you've provided is not causing the error, so there's no way we could debug it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文