Javascript AJAX 使用 jQuery - 语法错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8348476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AJAX using jQuery - Syntax Error?
提问by Deity
The following is the HTML containing the call to the JavaScript function responsible for issuing the AJAX call. I understand that anchor tags are not supposed to have a value attribute but I'm using it with jQuery's .attr("value") method.
以下是包含对负责发出 AJAX 调用的 JavaScript 函数的调用的 HTML。我知道锚标记不应该具有 value 属性,但我将它与 jQuery 的 .attr("value") 方法一起使用。
<a href="javascript:;" onclick="ajaxTest();" title="Execute AJAX" value="executeAJAX">Execute AJAX</a>
The following is the JavaScript function. If it is of any significance, it is contained in a .js file all by itself.
以下是 JavaScript 函数。如果它有任何意义,它本身就包含在一个 .js 文件中。
function ajaxTest() {
$.ajax({
type: "POST",
url: "doAJAX",
data: {"selectedScope": "5",
"selectedView": "6"},
dataType: "text",
success: function(responseData) {
$("#replaceThis").append(responseData);
}
});
}
Everytime the link is clicked, a "syntax error" message appears in Firefox's web console. The JavaScript seems to be working as intended, however.
每次单击该链接时,Firefox 的 Web 控制台中都会出现“语法错误”消息。然而,JavaScript 似乎按预期工作。
I just want to understand why I am getting the error.
我只是想了解为什么我会收到错误。
I should add that I'm using jQuery 1.7.1.
我应该补充一点,我使用的是 jQuery 1.7.1。
I've performed a search and the only resolution I've found was that the keys for the "data" option should be enclosed in double quotes so I've implemented that but I'm still getting the syntax.
我进行了搜索,我发现的唯一解决方法是“数据”选项的键应该用双引号括起来,所以我已经实现了,但我仍然得到了语法。
Thanks.
谢谢。
EDIT:
Looking at the Firebug console, the code above doesn't trigger an error like it did in Firefox's console, however, I saw the following in the XML part of the POST Request:
XML Parsing Error: syntax error Location: moz-nullprincipal:{1d13df07-25fb-4058-9f82-ce1bef3c8949} Line Number 1, Column 1:
alskdfjlaksjdfjasdfl
^The "alskdfjlaksjdfjasdfl" is simply what I've set up my servlet to return as I test this stuff out.
This is somewhat weird because it seems like jQuery is trying to parse the response as XML although I've explicitly stated it to be text.
编辑:
查看 Firebug 控制台,上面的代码不会像在 Firefox 的控制台中那样触发错误,但是,我在 POST 请求的 XML 部分看到了以下内容:
XML解析错误:语法错误位置:moz-nullprincipal:{1d13df07-25fb-4058-9f82-ce1bef3c8949}第1行,第1列:
alskdfjlaksjdfjasdfl
^“alskdfjlaksjdfjasdfl”只是我设置我的 servlet 在我测试这些东西时返回的内容。
这有点奇怪,因为 jQuery 似乎试图将响应解析为 XML,尽管我已经明确指出它是文本。
回答by Adam
I recently encountered the same issue. jQuery appeared to be handling the data and the dataType correctly, but instead it was Firefox returning the syntax error, which explains why your code was executing as intended but still printing an error to the console.
我最近遇到了同样的问题。jQuery 似乎正确处理数据和数据类型,但 Firefox 返回了语法错误,这解释了为什么您的代码按预期执行但仍向控制台打印错误。
If you look in the developer console, you can see that Firefox is interpreting the plain text data as another format (likely XML). Firefox tires to parse the data as XML, but can't because it's not valid XML which results in "Syntax error" being printed to the console.
如果您查看开发人员控制台,您可以看到 Firefox 将纯文本数据解释为另一种格式(可能是 XML)。Firefox 厌倦了将数据解析为 XML,但不能,因为它不是有效的 XML,导致“语法错误”被打印到控制台。
The resolution to this problem for me was editing the server so it returned the below header:
对我来说,解决此问题的方法是编辑服务器,因此它返回以下标题:
Content-Type: "text/plain"
This only appeared to be an issue with Firefox, Chrome did not encounter this issue. There is a Firefox bug herewhich seems to touch on the issue.
这似乎只是 Firefox 的问题,Chrome 没有遇到此问题。这里有一个Firefox 错误,似乎涉及到这个问题。
回答by Diodeus - James MacFarlane
function ajaxTest() {
$.ajax({
type: "POST",
url: "doAJAX",
data: {"selectedScope": "5",
"selectedView": "6" <---- here (drop comma, add bracket)
},
dataType: "text",
success: function(responseData) {
$("#replaceThis").append(responseData);
}
});
}
回答by Christofer Eliasson
It's your data-object that is the problem, you are missing a trailing }
问题在于您的数据对象,您缺少尾随 }
Edit:
编辑:
Not sure if its the problem, but I think you can skip the quotation around your keys in the data-object (and around the values as well if you only intend to send numbers, keep them if you intend to send strings for instance):
不确定是否是问题,但我认为您可以跳过数据对象中键周围的引号(如果您只想发送数字,也可以跳过值周围的引号,例如,如果您打算发送字符串,请保留它们):
Edit 2:
编辑2:
According to jQuery documentation, .append() expects a DOM element, HTML string, or jQuery object. Thus, I've created a DOM text-node of your response, and append that instead of just the text string. Note that the edit is untested.
根据jQuery 文档, .append() 需要一个 DOM 元素、HTML 字符串或 jQuery 对象。因此,我为您的响应创建了一个 DOM 文本节点,并附加了它而不仅仅是文本字符串。请注意,该编辑未经测试。
function ajaxTest() {
$.ajax({
type: "POST",
url: "doAJAX",
data: {
selectedScope: 5,
selectedView: 6
},
dataType: "text",
success: function(responseData) {
$("#replaceThis").append(document.createTextNode(responseData));
}
});
}
回答by Yes Barry
function ajaxTest() {
$.ajax({
type: "POST",
url: "doAJAX",
data: {
"selectedScope": "5",
"selectedView": "6"
}, // <-- need closing curly brace and comma
dataType: "text",
success: function(responseData) {
$("#replaceThis").append(responseData);
}
});
}
EDIT
编辑
I got it working here on jsFiddle.
我让它在 jsFiddle 上工作。
Additionally, try changing
此外,尝试更改
<a href="javascript:;" ...
to
到
<a href="javascript:void();"...`
EDIT 2
编辑 2
I got it working an alternative way as well. (using Firefox 8.0.1 and Jquery 1.7.1)
我也让它以另一种方式工作。(使用 Firefox 8.0.1 和 Jquery 1.7.1)
回答by Icarus
That's because you forgot to put a }
on the data
parameter. Try this:
那是因为你忘记}
在data
参数上加上 a 。尝试这个:
function ajaxTest() {
$.ajax({
type: "POST",
url: "doAJAX",
data: {"selectedScope": "5",
"selectedView": "6"},
dataType: "text",
success: function(responseData) {
$("#replaceThis").append(responseData);
}
});
}
One recommendation: When you have issues like this. Use the Google Closure Compiler Service. It will tell you exactly where your problem is. Or Firebug, if you use Firefox.
一项建议:当您遇到此类问题时。使用Google Closure Compiler Service。它会准确地告诉您问题出在哪里。或Firebug,如果您使用 Firefox。
回答by ahmed hamdy
function ajaxTest() {
$.ajax({
type: "POST",
url: "doAJAX",
dataType: "html",
data:{
"selectedScope": "5",
"selectedView": "6"
},
success: function(responseData) {
$("#replaceThis").append(responseData);
}
});
}