Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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' );
}

        });
like image 258
Hamid Avatar asked Jun 05 '13 06:06

Hamid


2 Answers

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.

like image 88
Grigory Kislin Avatar answered Oct 25 '22 05:10

Grigory Kislin


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.

like image 45
Kenny Avatar answered Oct 25 '22 03:10

Kenny