最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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 debugger and still ajax is showing error. XML Parsing Error: no element found Location: moz-nullprincipal:{6b0a1ac2-50ab-4053-9f71-8ae49202288d} Line Number 1, Column 1:

            $j.ajax({

            type:"POST",
            url:'.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 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 debugger and still ajax is showing error. XML Parsing Error: no element found Location: moz-nullprincipal:{6b0a1ac2-50ab-4053-9f71-8ae49202288d} Line Number 1, Column 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' );
}

        });
Share Improve this question edited Oct 24, 2013 at 11:52 Vinoth Krishnan 2,9496 gold badges30 silver badges35 bronze badges asked Jun 5, 2013 at 6:25 HamidHamid 1291 gold badge3 silver badges9 bronze badges 2
  • check this link: tharindumathew.com/2012/05/16/xml-parsing-error-in-firebug – Prasad Jadhav Commented Nov 18, 2013 at 5:31
  • the link is now at: mackiemathew.wordpress.com/2012/05/16/… – dev_nut Commented Oct 20, 2014 at 21:06
Add a comment  | 

6 Answers 6

Reset to default 6

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

Fixed by remove dataType : "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.

Refer to the jQuery.ajax() documentation: 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:

$.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.

I had the same problem when I made GET XMLHttpRequest call.

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

It was fixed by setting ContentType on the HttpServletResponse.

 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.

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

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.

发布评论

评论列表(0)

  1. 暂无评论