简体   繁体   English

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

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

i am unable to get the response from ajax.我无法从 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 debugger and still ajax is showing error.请指导我如何解决此错误,我已成功从服务器返回数据,我已在Fiddle Web 调试器中对其进行了检查,但 ajax 仍然显示错误。 XML Parsing Error: no element found Location: moz-nullprincipal:{6b0a1ac2-50ab-4053-9f71-8ae49202288d} Line Number 1, Column 1: 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' );
}

        });

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.但现在成功回调被调用。

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).从 jQuery 1.5.1 开始,jqXHR 对象还包含 overrideMimeType() 方法(它在 jQuery 1.4.x 中也可用,但在 jQuery 1.5 中被暂时删除)。 The .overrideMimeType() method may be used in the beforeSend() callback function, for example, to modify the response content-type header: .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.对 $.ajax() 调用的不同类型的响应在传递给成功处理程序之前要经过不同类型的预处理。 The type of pre-processing depends by default upon the Content-Type of the response, but can be set explicitly using the dataType option.默认情况下,预处理的类型取决于响应的 Content-Type,但可以使用 dataType 选项显式设置。 If the dataType option is provided, the Content-Type header of the response will be disregarded.如果提供了 dataType 选项,则响应的 Content-Type 标头将被忽略。

I had the same problem when I made GET XMLHttpRequest call.当我进行GET XMLHttpRequest调用时,我遇到了同样的问题。

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");

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 时。

I added ContentType to ResponseEntity and problem solved.我将 ContentType 添加到 ResponseEntity 并解决了问题。 Try to add a valid ContentType to your controller response:尝试将有效的 ContentType 添加到您的控制器响应中:

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

I just ran into the same problem with a simple request:我刚刚通过一个简单的请求遇到了同样的问题:

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

In my case, this was a fire-and-forget scenario - I don't care about the response.就我而言,这是一个即发即忘的场景——我不在乎回应。 To solve it, it was a case of changing the server-side code to correctly return a 204: No content status code, instead of returning 200: OK .为了解决它,这是一个更改服务器端代码以正确返回204: No content状态代码,而不是返回200: OK

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM