javascript ajax 响应错误(XML 解析错误:找不到元素位置:moz-nullprincipal)

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

ajax response error(XML Parsing Error: no element found Location: moz-nullprincipal)

javascriptajaxjquery

提问by Hamid

i am unable to get the response from ajax. please guide me how to resolve this error, i am getting successful data return from the server i have checked it in fiddle web debuggerand still ajax is showing error. XML Parsing Error: no element found Location: moz-nullprincipal:{6b0a1ac2-50ab-4053-9f71-8ae49202288d} Line Number 1, Column 1:

我无法从 ajax 得到响应。请指导我如何解决此错误,我从服务器成功返回数据,我已经在fiddle web 调试器中检查过它,但 ajax 仍然显示错误。 XML 解析错误:未找到元素位置:moz-nullprincipal:{6b0a1ac2-50ab-4053-9f71-8ae49202288d} 第 1 行,第 1 列:

            $j.ajax({

            type:"POST",
            url:'http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit',
            data: 'Celsius=12',
            crossDomain:true,
            async: false,
            success:function(response)
            {
                alert("Success Full Done"+response.string);
            },
            beforeSend: function( xhr ) {
 xhr.overrideMimeType( 'text/plain; charset=UTF-8' );
}

        });

回答by Grigory Kislin

I've this problem with request:

我有这个请求问题:

$.ajax({
    type: "POST",
    url: ajaxUrl,
    dataType : "json",
    contentType: "application/json",
    data: JSON.stringify(data),
    success: function (data) {
         ...
    }
});

Accept header in request is:

请求中的接受标头是:

Accept  application/json, text/javascript, */*; q=0.01

Response status is 200, but browser detect error and no success callback called

响应状态为 200,但浏览器检测到错误且未调用成功回调

Fixed by remove dataType : "json":

通过删除数据类型修复:“json”:

$.ajax({
    type: "POST",
    url: ajaxUrl,
    contentType: "application/json",
    ...

The only difference that accept header in request changed to:

请求中接受标头的唯一区别更改为:

Accept  */*

But now success callback is called.

但现在成功回调被调用。

回答by Kenny

Add the "beforeSend" function into your AJAX call to override the acceptable response mime type.

将“beforeSend”函数添加到您的 AJAX 调用中以覆盖可接受的响应 mime 类型。

Refer to the jQuery.ajax() documentation: http://api.jquery.com/jquery.ajax/

参考 jQuery.ajax() 文档:http: //api.jquery.com/jquery.ajax/

As of jQuery 1.5.1, the jqXHR object also contains the overrideMimeType() method (it was available in jQuery 1.4.x, as well, but was temporarily removed in jQuery 1.5). The .overrideMimeType() method may be used in the beforeSend() callback function, for example, to modify the response content-type header:

从 jQuery 1.5.1 开始,jqXHR 对象还包含 overrideMimeType() 方法(它在 jQuery 1.4.x 中也可用,但在 jQuery 1.5 中被暂时删除)。.overrideMimeType() 方法可以在 beforeSend() 回调函数中使用,例如,修改响应 content-type 头:

$.ajax({
  url: "http://fiddle.jshell.net/favicon.png",
  beforeSend: function( xhr ) {
    xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  }
})
  .done(function( data ) {
    if ( console && console.log ) {
      console.log( "Sample of data:", data.slice( 0, 100 ) );
    }
  });

And:

和:

Data Types

数据类型

Different types of response to $.ajax() call are subjected to different kinds of pre-processing before being passed to the success handler. The type of pre-processing depends by default upon the Content-Type of the response, but can be set explicitly using the dataType option. If the dataType option is provided, the Content-Type header of the response will be disregarded.

对 $.ajax() 调用的不同类型的响应在传递给成功处理程序之前要经过不同类型的预处理。默认情况下,预处理的类型取决于响应的 Content-Type,但可以使用 dataType 选项显式设置。如果提供了 dataType 选项,则响应的 Content-Type 标头将被忽略。

回答by avneet kaur

This error may be caused due to two reason. One is when getting no response from java backend and other is when their is no @ResponseBody in java Controller method.

此错误可能由两个原因引起。一种是从 java 后端没有得到响应,另一种是当它们在 java Controller 方法中没有 @ResponseBody 时。

回答by Hopecee

I had the same problem when I made GETXMLHttpRequestcall.

GETXMLHttpRequest打电话时遇到了同样的问题。

var req = new XMLHttpRequest();
req.open('GET', '/exampleServlet', false);
req.send(null);

It was fixed by setting ContentType on the HttpServletResponse.

它是通过在 HttpServletResponse 上设置 ContentType 来修复的。

 response.setContentType("text/plain");

回答by sparse

I added ContentType to ResponseEntity and problem solved. Try to add a valid ContentType to your controller response:

我将 ContentType 添加到 ResponseEntity 并解决了问题。尝试将有效的 ContentType 添加到您的控制器响应中:

ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).build();

ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).build();

回答by Andrew Ramshaw

I am going to be straight up with this after some years of experience: your problem may simply be the environment you've chosen to program in. I have two development environments- VS Code and Netbeans- and for some reason which I don't know ajax and other methods work in one and not the other. I do not know why this is, but with VS Code ajax post calls will just not work, but when the EXACT SAME CODE is used in an IDE like the most recent version of NetBeans it works EXACTLY as it is programmed to do. Test your code across MULTIPLE coding environments because it would not surprise me if you were being screwed over with this for HOURS just because of some stupid coding thing that was happening somewhere else like I was. Your code may be FINE, but these errors lead to weird fixes that may not really be fixes but just weird glitch workarounds. Your code is most likely just fine as it is, and this is why I hate when a product as good as VS Code has these glitches because as great as it is it messes with your concept of your ability to program and stalls you from being able to progress healthily along with your progress. No offense to them, but these glitches SUCK...

经过多年的经验,我将直接解决这个问题:您的问题可能只是您选择的编程环境。我有两个开发环境 - VS Code 和 Netbeans - 出于某种原因,我没有知道 ajax 和其他方法在一种而不是另一种中起作用。我不知道为什么会这样,但是使用 VS Code ajax post 调用将不起作用,但是当在 IDE 中使用完全相同的代码(如 NetBeans 的最新版本)时,它完全按照编程的方式工作。在多个编码环境中测试您的代码,因为如果您因为像我这样的其他地方发生的一些愚蠢的编码事情而被搞砸了 HOURS,我不会感到惊讶。您的代码可能很好,但这些错误会导致奇怪的修复,这些修复可能不是真正的修复,而只是奇怪的故障解决方法。你的代码很可能就是这样,这就是为什么我讨厌像 VS Code 这样好的产品有这些故障,因为它会干扰你编程能力的概念,并阻碍你的能力随着你的进步健康进步。无意冒犯他们,但这些小故障很糟糕......