Home  >  Article  >  Backend Development  >  javascript - ajax does not go to success, only error, and the request status code is 200.

javascript - ajax does not go to success, only error, and the request status code is 200.

WBOY
WBOYOriginal
2016-09-11 11:34:081304browse

How to solve it? It is OK to move another domain name, but this domain name will not be moved. The front-end code is as follows:

javascript - ajax does not go to success, only error, and the request status code is 200.

The backend code is as follows:

javascript - ajax does not go to success, only error, and the request status code is 200.

javascript - ajax does not go to success, only error, and the request status code is 200.

Reply content:

How to solve it? It is OK to move another domain name, but this domain name will not be moved. The front-end code is as follows:

javascript - ajax does not go to success, only error, and the request status code is 200.

The backend code is as follows:

javascript - ajax does not go to success, only error, and the request status code is 200.

javascript - ajax does not go to success, only error, and the request status code is 200.

The interface doesn’t handle callbacks, right?

The writing is correct. It is estimated that the server returned something other than the jsonp format, and the callback processing failed. The default
jsonpcallback parameter of $.ajax is ?callback=_XXXX. Check whether the background processing and return are correct.

The value returned by the server is wrong. You are returning a null value. It looks like if there is something like {"result":"successful"}, try something like that?

Javascript: Add

<code class="javascript">$.ajax({
    ....
    jsonp:'jsonp_callback',  <- 指定callback回调函数名
    ....
})</code>

jQuery will use AJAX GET to request a /url/?callback=jsonp_callback URL
and then execute the jsonp_callback(result)JavaScript callback

<code class="php">function(){
    return string;
}</code>

What javascript receives will be a String type output. It's not JSON. If it’s not JSON, it’s DIE.

<code class="php">function (){
    // 这里是callback还是jsonp_callback忘了。具体百度一下
    return $_GET['callback']."(".json_encode($string).")";
    // 实际返回并输出的将会是:  `jsonp_callback("string")`
}</code>

update

javascript - ajax does not go to success, only error, and the request status code is 200.

Look at the picture and talk:
The ajax request address is http://baidu.com
The actual request address is http://baidu.com/?callback=xxxxxxx&_=xxx&_=xxxx

Why not use numbers instead of returned content?

dataType does not match the return value. It is recommended to remove this parameter and let jquery automatically recognize the return value.
Also, this is not jsonp, right?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn