Ajax requests return 200 but do not enter success-2

Source: Internet
Author: User
Java code
  1. After the previous Ajax operation using jquery, the program request was successful:
  2. 1. Status Code 200 is returned, indicating that the server has responded to the client request normally;
  3. 2. The httpwatcher of firebug and IE shows that the server returns normal data and the data conforms to the business logic.
  4. However, the program does not go to the callback function success: function (data) {*****}, but to the error: function (data ){***}
  5. Remember that the last time was caused by cross-origin access problems. This check shows that there is no cross-origin issue. At this time it is very puzzled.
  6. The source of the incident is this: one of the backend configuration management modules is about internationalization configuration, adding internationalization descriptions, and so on.
  7. The source of the problem is that the data can be normally displayed when you enter key = 'A' to query the first 10 pieces of data, but when I input key value Z, when the first 20 pieces of data are queried, it is found that the data cannot be displayed, but the server returns the data in the database. At this time, the first response is that there is a problem with the data returned from time to time. A rough check shows that the returned data is not significantly different from the first query. However, when only 14th pieces of data are queried, it cannot be displayed. At this time, I began to doubt the data problem, and then went to the database to find 14th pieces of data, and found nothing special.
  8. At this moment, I began to doubt whether the Javascript program has a problem with data processing compatibility. I think it is incredible. After about half an hour, it is increasingly unlikely. I gave up this idea.
  9. Turn to review data. However, I found that the data was broken from the middle line, so I didn't care too much about it. After struggling for a while, I asked a colleague, pointing out that the data may have an additional "Enter key ", go to the database table and check the data again. One field value has an additional "Enter key ". After deletion, everything becomes normal.



Java code

  1. [Align = left] [color = Red] thinking about the first and second problems, I initially realized:
  2. [B] 1. The returned data type must comply with the defined data type. That is to say, if the datatype you define is of the JSON type, the returned data must be of the JSON type. If not, the program blocks in the error will be executed. [/B] [color = Red]
  3. (1) Pay special attention to whether the returned JSON data is in strict JSON format.
  4. (2) It should also be seriously concerned that when a list data is returned in the background (the data in the list is in JSON format), there is no dirty data, that is, not a strict JSON format.
  5. It is concealed that a data field contains special characters at the beginning or end, such as "Enter key" and "tab key ".
  6. The avoidance of such hidden errors is a good programming habit:
  7. A. It is best to set the data to be written into the database on the page, and to perform null Operations <use the client best>.
  8. B. special characters are required for special businesses.
  9. This is not enough because the testing or manually adding data to the database by the developer during the development process may add spaces and so on, resulting in program debugging and testing troubles. At this time, you need to consider processing the obtained data in the background code. [/Color] [/align] [/color]
  10. [B]
  11. 2. the reason is that the Ajax request is cross-origin. The solution is to add a JS: [/B] Document in both files. domain, or use the jsonp method, as mentioned in my previous blog.



Java code

  1. Another point of understanding about Ajax in jquery is that the client initiates a request to obtain 200 for the server, and <get the server response data correctly> no problem. at this time, before judging whether the callback function corresponding to success is entered or the callback function corresponding to error, some items may be checked:
  2. 1. Whether the returned data is the data type defined in datatype. If some data is not or even a piece of data is not strictly defined according to the datatype type, the program will enter error: function (){****}
  3. 2. Whether the requested domain is the same as the current domain. If it is not the same domain, it is very likely to enter error: function (){***}
Related Article

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.