Javascript jQuery Ajax 调用 No element found 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3334969/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:13:45  来源:igfitidea点击:

jQuery Ajax call No element found issue

javascriptjquery

提问by nimi

I am getting a javascript error on firefox 3.5, when trying to call an ajax method.

尝试调用 ajax 方法时,我在 firefox 3.5 上遇到 javascript 错误。

Please find the error below:

请找出以下错误:

XML Parsing Error: no element found Location: moz-nullprincipal:{1a2c8133-f48f-4707-90f3-1a2b2f2d62e2} Line Number 1, Column 1:

^

this is my javascript function:

这是我的 javascript 函数:

function Update(Id) {
    $.ajax({
        type: "GET",
        url: ROOT_URL + "/sevice/udates.svc/Update?Id=" + Id,
        success: function(response) {

        },
        async: false
    });
}

采纳答案by Gabriele Petrioli

The ajax call expects XML back (perhaps due to bad guessing) and tries to parse it and fails if nothing is returned or it is not valid XML..

ajax 调用期望返回 XML(可能是由于错误的猜测)并尝试解析它并在没有返回任何内容或它不是有效的 XML 时失败。

Use the dataTypeoption to specify the format of the response.

使用该dataType选项指定响应的格式。



From the comments it looks like some browsers cannot handle an no-content response. So, a workaround for such cases might be to return something from your service (even a single space).

从评论看来,某些浏览器无法处理无内容响应。因此,针对此类情况的解决方法可能是从您的服务中返回某些内容(甚至是单个空格)。

回答by mimo

I fixed the problem by setting mimeType to "text/html"

我通过将 mimeType 设置为“text/html”来解决这个问题

回答by CharlesA

I've come across an alternative cause of this - might help someone.

我遇到了另一个原因 - 可能会帮助某人。

If you make a $.ajaxrequest (in my case a PUTrequest) that returns a 200 header but no body content I've seen this same XML Parsing error message occur - even when the dataTypeis set to json.

如果你发出一个$.ajax请求(在我的例子中是一个PUT请求),它返回一个 200 标头但没有正文内容,我见过同样的 XML 解析错误消息出现 - 即使dataType设置为json.

(At least) two solutions work:

(至少)两种解决方案有效:

  • Make all API PUTrequests return some content, or
  • Return a 204 'No Content' header instead (what I ended up doing)
  • 使所有 API PUT请求返回一些内容,或
  • 改为返回 204 'No Content' 标头(我最终做了什么)

回答by randall

It's a known FireFox bug. https://bugzilla.mozilla.org/show_bug.cgi?id=547718to quick fix this , you maybe can return an response with html structure(but no content).

这是一个已知的 FireFox 错误。 https://bugzilla.mozilla.org/show_bug.cgi?id=547718要快速解决此问题,您也许可以返回带有 html 结构(但没有内容)的响应。

回答by Teja Kantamneni

asyncis also part of options. Also specify the dataTypeas xml

async也是选项的一部分。还指定dataTypexml

function Update(Id) {
    $.ajax({
        type: "GET",
        async: false,
        dataType: "XML",
        url: ROOT_URL + "/sevice/udates.svc/Update?Id=" + Id,
        success: function(response) {

        }
    });
}

回答by Stamen

You need to send html document to the output (the output udates.svc in your case) . If you use ASP.NET, you could do the following:

您需要将 html 文档发送到输出(在您的情况下为输出 udates.svc)。如果您使用 ASP.NET,则可以执行以下操作:

Response.Clear();
Response.Write("<html xmlns=”http://www.w3.org/1999/xhtml”>");
Response.Write("<head><title></title></head>");
Response.Write("<body>");
Response.Write("your output");
Response.Write("</body>");
Response.Write("</html>");
Response.ContentType = "text/HTML";
Response.End();