Ajax response error (XML parsing error: no element was found Location: moz-nullprincipal)

I can not get a response from ajax. please help me how to solve this error, I get a successful return of data from the server, I checked it in the web debugger fiddle and still ajax shows the error. XML parsing error: element not 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' ); } }); 
+6
source share
6 answers

I have this problem with the request:

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

Confirm the title in the request:

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

The response status is 200, but a browser detection error and rejection of a successful call

Fixed deleting dataType: "json":

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

The only difference that accepts the header request in the request has changed to:

 Accept */* 

But now the success callback is called.

+4
source

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

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

As in jQuery 1.5.1, the jqXHR object also contains the overrideMimeType () method (it was also available in jQuery 1.4.x, but was temporarily removed in jQuery 1.5). The .overrideMimeType () method can be used in the beforeSend () callback function, for example, to change the header of the response 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 responses to the $ .ajax () call undergo various types of preprocessing before passing success to the handler. The preprocessing type depends on the Content-Type by default on the response, but can be explicitly set using the dataType option. If the dataType parameter is specified, the Content-Type header of the response will be ignored.

+2
source

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

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

It has been fixed by setting ContentType in the HttpServletResponse.

  response.setContentType("text/plain"); 
+1
source

This error can be caused for two reasons. One of them is when a response from the Java backend is not received, and the other when it does not have @ResponseBody in the java Controller method.

+1
source

I added ContentType to ResponseEntity and the problem is resolved. Try adding a valid ContentType to your controller response:

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

0
source

I am going to deal with this after several years of experience: your problem may simply be the environment in which you decide to program. I have two development environments — VS Code and Netbeans — and for some reason I don’t put on Ajax and the other methods work in one and not the other. I don’t know why this is so, but using VS code, ajax post-calls simply won’t work, but when the IDE uses the same exact code as the latest version of NetBeans, it works exactly as programmed. Test your code in MULTIPLE encoding environments because it won’t surprise me if you messed up with this for HOURS just because of some kind of silly coding thing that was happening somewhere else like me. Your code may be FINE, but these errors lead to strange fixes, which may not be fixes, but simply strange workarounds. Most likely, your code is in order, and that’s why I hate it when a product like VS Code has such glitches, because although it’s great, it mixes with your concept of programming ability and doesn’t give you Healthy progress opportunities along with your progress. No offense to them, but these glitches suck ...

0
source

All Articles