Code Note: Ajax bizarre error-Request status is 200

Source: Internet
Author: User


Weird jquery ajax Satus 200 Error


Today, I do not know how to solve the problem. In this case, it is not because jquery's Ajax writing failed, nor did the request fail, what is it?


1. jquery's main parameters for Ajax requests


Beforesend: Before sending an AJAX request



Success: Send AJAX request successfully



Error: Sending Ajax request errors, usually caused by network loss of connection, server error, background method error, etc.


2, the current phenomenon


Its own code is like this.


......
$.ajax ({
url:path+ "/**",
timeout:8000,
Cache:false,
Type: "Post",
DataType: "Json/html",
Data: {"Txtreal": TR, "Txtcard": TC, "Txtphone": TP, "Txtphonecode": tpcod},//' adm:reg20160616 ', "Txtphone", " Txtphonecode "
Error:function (d) {
Alert ("Error");
alert (Curpath);
Alert ($ ("#txtReal"). Val ());
Alert (TC);
alert (TP);
alert (TPCOD);
},
Success:function (d, S, R) {
if (d) {
if (d.status==0) {
$.jbox.tip (D.message, "fail");
}else{
Window.location.href= "/member/bank/bindingaccount";
}
}
}
});


......



To be able to copy the test at any time, write it with the following code



To reproduce an AJAX request, the following is provided only.



$(function()



{



$. Ajax(



{



URL:location. href,



DataType:' json ',



Type:' post ',



Data: {action:' _yundanran-all-cat '},



Beforesend:function()



{



Console. Log(' beforesend ');



},



Success:function()



{



Console. Log(' success ');



},



Error:function()



{



Console. Log(' error ');



},



Complete:function()



{



Console. Log(' complete ');



}



});



});



You can copy this piece of code to the console to run the test, and the result is this:



Red selected area, error, indicating that jquery's Ajax request failed , the failed code is Textstatus:parsererror, then we can see what the actual request is:






If the red flag, the request status is 200, indicates that the request was successful and that the parameter action was sent correctly. Look at what the return results are:






The result of the return is indeed true.



It's strange that the AJAX request is correct and the content is returned correctly. But the Ajax function of jquery is to judge the wrong request. And this strange phenomenon began last night (November 6, 2012), has been very normal.


3. Solutions
    • 1. Delete the output part of the background program .




    • 2. Add exit () at the end of the Ajax output to prevent additional content from being exported.


Such as:



JSON Format header information



Header("Content-type:application/json");



Output JSON content



echo Json_encode($json);



Exit End Program Run



Exit;






Reprinted from: http://qianduanblog.com/post/jquery-ajax-status-success-200.html








Status Value description for Ajax



1, status=304



304 is not an error, 304 is the server tells you that the URL you requested, the content has not changed, the browser can directly use the local cache, to avoid wasting network traffic, improve user response speed



Ajax is taken from the local cache when it is requested by the Get method, and the path parameter is the same, and if it is taken, it will not request the background.



If you want to avoid getting a 304 response, you can add a random parameter, such as a JS code, to the requested URL:
url = url + "&" + new Date ();



2, status=302




$.ajax ({
url:curpath+ "/regfinal",
timeout:8000,
Cache:false,
Type: "Post",
DataType: "Json/html",
Data: {"Txtreal": TR, "Txtcard": TC, "Txtphone": TP, "Txtphonecode": tpcod},//' adm:reg20160616 ', "Txtphone", " Txtphonecode "
............



When the server sends a 302 response to the browser, the browser does not directly perform the AJAX callback processing, but instead performs a 302 redirect-reading the location information from the response headers and then making a request to the URL in the location. The Ajax callback is not processed until the response to this request is received. The approximate process is as follows:



Ajax Browser, 302, browser (redirect), server, browser, Ajax



In our test program, because the redirect URL returned by 302 does not have a corresponding handler on the server, the Ajax callback function is given a 404 status code, and if there is a corresponding URL, the resulting status code is 200.



So, if you want to redirect through Location.href in an AJAX request based on a 302 response, it's not feasible.



You can continue with Ajax, modify server-side code, and change the original 302 response to a JSON response.






3, status=200



A status code of 200 indicates a successful resource acquisition.


When the browser loads the resource for the first time, the return is typically 200, which means that the resource is successfully obtained and the Max-age is recorded in the browser's cache, and the second visit:
If just open with the browser, then the browser will be to determine whether the resource in the cache, if there is, will judge Max-age, see if there is no expiration, if not expired, then directly read the cache, will not interact with the server, in other words, broken network can open, and local run the same! If it has expired, then go to the server request, waiting for the server response, this is time-consuming, if the server found that the resource has not changed, then it will return 304, tell the browser, I have not changed, you read the cache, so the browser does not have to pull the data from the server, however, Waiting for the server response is also a very deadly problem, in the network developed today, waiting for a response, sometimes slower than downloading.
If the browser is refreshed, then the browser will not Judge Max-age, go directly to the server to take, if the server to determine the resources have not changed, will still return 304, and the above is the same, so refresh, in fact, is very scary, equal to all the resources to go to the server side, Ask the server if I have expired.
In summary, minimize the amount of resources on the Web page! Try to merge JS CSS images! The response rate will soar!
Today, the response speed is more important than Internet speed!!











Date written: 2016-6-17 15:59:25



Code Note: Ajax bizarre error-Request status is 200


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.