AJAX使用jQuery - 语法错误? [英] AJAX using jQuery - Syntax Error?

查看:154
本文介绍了AJAX使用jQuery - 语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是包含对负责发出AJAX调用的JavaScript函数的调用的HTML。我知道锚标签不应该有值属性,但我使用jQuery的.attr(value)方法。

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.

<a href="javascript:;" onclick="ajaxTest();" title="Execute AJAX" value="executeAJAX">Execute AJAX</a>

以下是JavaScript函数。如果它有任何意义,它本身就包含在.js文件中。

The following is the JavaScript function. If it is of any significance, it is contained in a .js file all by itself.

function ajaxTest() {
    $.ajax({
        type: "POST",
        url: "doAJAX",
        data: {"selectedScope": "5",
               "selectedView": "6"},
        dataType: "text",
        success: function(responseData) {
            $("#replaceThis").append(responseData);
        }
    });
}

每次点击链接时,都会出现语法错误消息在Firefox的Web控制台中。但是,JavaScript似乎按预期工作。

我只想了解我收到错误的原因。

I just want to understand why I am getting the error.

我应该补充说我正在使用jQuery 1.7.1。

I should add that I'm using 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.

谢谢。


编辑:

查看Firebug控制台,上面的代码不会像在Firefox的控制台中那样触发
错误,但是,我在POST请求的XML部分看到了以下

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解析错误:语法错误位置:
moz-nullprincipal:{1d13df07-25fb-4058-9f82-ce1bef3c8949}行号
1,第1列:

alskdfjlaksjdfjasdfl

^

alskdfjlaksjdfjasdfl就是我的意思我设置我的servlet返回,因为我测试这些东西。

The "alskdfjlaksjdfjasdfl" is simply what I've set up my servlet to return as I test this stuff out.

这有点奇怪,因为看起来jQuery试图解析
响应为XML虽然我已明确表示它是文本。

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.


推荐答案

我最近遇到了同样的问题。 jQuery似乎正在正确地处理数据和dataType,而是Firefox返回语法错误,这解释了为什么你的代码按预期执行但仍然向控制台打印错误。

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.

如果查看开发人员控制台,可以看到Firefox正在将纯文本数据解释为另一种格式(可能是XML)。 Firefox厌倦了将数据解析为XML,但不能,因为它不是有效的XML导致语法错误被打印到控制台。

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.

对此的解析我的问题是编辑服务器,所以它返回下面的标题:

The resolution to this problem for me was editing the server so it returned the below header:

Content-Type: "text/plain"

这似乎只是Firefox的一个问题,Chrome没有遇到这个问题。这里有一个 Firefox bug ,似乎触及了这个问题。

This only appeared to be an issue with Firefox, Chrome did not encounter this issue. There is a Firefox bug here which seems to touch on the issue.

这篇关于AJAX使用jQuery - 语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆