Front desk ajax request PHP backend return success but into the error method to solve

Source: Internet
Author: User

In recent days have been solving the error problem, the weekend has not been able to live, grief, want to escape?? .....

First, the beginning of grief

Project with jquery Ajax method request PHP background, foreground code write no problem, back in the background JSON data is probably right (guess, later verified that JSON is right), and then the tragedy is the same;

First look at the core code:

$.ajax ({type:"POST", URL:"{: U (' City/adds ')}", data:{}, DataType:' JSON ', Success:function(data) {if(Data.status = = 1 ){Layer.msg (' Success * ̄▽ ̄* '); SetInterval (function(){                                    varindex =Parent.layer.getFrameIndex (window.name);                                    Parent.layer.close (index);                                Window.parent.location.reload (); },1000)                            }Else{Layer.msg (' Failure >﹏< ');}}, Error:function(XMLHttpRequest, Textstatus, Errorthrown) {//console.log (xmlhttprequest.status);                            Console.log (xmlhttprequest.readystate);                        Console.log (Textstatus); }                    })        

  Problem One : The request successfully entered the success method, but the following error occurred,

inch JSON at position 0

Turn over the network panel and see that the data for the request response is <pre>array (3) {...} </pre> string, continue to endure ...

Start analysis and find solutions;

Error code is "JSON location 0 found unrecognized tag <", meaning that the response data is not JSON data, then a variety of methods to try;

First try to change the front-end code, do not specify the ' DataType ', in the success method to remove the <PRE></PRE>, the discovery will not be error, but the problem and secondly;

Remove the <pre></pre>, using the following: JQuery (data). Text ()

  question two : the request succeeds but always enters the error method without any error;

  

Preview is JSON data, calibration without any problems;

  Response found in front of the data more than a little red dot ·, is the data problem, Ajax is not to parse, has been into the error method;

Then consult with the backstage, the returned data has a problem, need to pay attention to the format;

I went to Baidu search Ah, found really background data problems, did not pay attention to the red dot problem;

Empty the output with method Ob_clean () before returning data to the backend, the returned type is object and is no longer a string, thus succeeding ~ ~

Two, happy ending, wordy chat a few words

Problem I and problem II can be solved in the background with Ob_clean () .

The problem is solved at the foreground, but the data volume is not applicable, so use caution; you can let the background try Echo json_encode ($data, true);  Echo Json_encode ($obj, Json_unescaped_unicode); or oneself to ~ The method is various, always has the suitable one;

Summing up question two, is a problem with the JSON data is not rigorous, leading to the foreground parsing error.

JSON data is not rigorous enough to say, such as a full-width and half-width of the space, single double quotes {k:v}, JSON comment statements, and other small problems will be caused; if we meet the same problem, but can not be solved in the same way, we must find various reasons, Not limited to the AJAX code part is not written wrong, the background is really json, but to find type/parsing/writing/null value analysis, different code different analysis mode;

The last hard to say is who the pot, not good, or need to work with the front end and the background to better and faster completion??。

Record the process of this resolution, thank you all ~??

  

  

Front desk ajax request PHP backend return success but into the error method to solve

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.