Home >Backend Development >PHP Tutorial >javascript - jQuery ajax跨域问题

javascript - jQuery ajax跨域问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:42:501034browse

遇到如下情况 使用jQuery jsonp 跨域成功后为什么会执行 error 方法 具体代码如下:
HTML部分

<code>    $.ajax({
        type:'get',
        dataType:'jsonp',
        url:"http://XXXX.com/test.php?jsoncallback=success_jsonpCallback", //调用自定义函数
        success:function(msg){
            alert(msg);
        },
        error:function(msg){
            alert(123);
            alert(msg);
        }
    });


    function success_jsonpCallback (result) {
        alert(result.img_url);
    }
</code>

PHP文件(test.php)

<code>$data['status'] = "1";
$data['img_url'] = "http://www.baidu.com/img/baidu_jgylogo3.gif";
$jsoncallback = $_GET['jsoncallback'];
//var_dump($_GET['jsoncallback']);
echo $jsoncallback."(".json_encode($data).")";
</code>

此外 希望有前端大神能给予解答.

回复内容:

遇到如下情况 使用jQuery jsonp 跨域成功后为什么会执行 error 方法 具体代码如下:
HTML部分

<code>    $.ajax({
        type:'get',
        dataType:'jsonp',
        url:"http://XXXX.com/test.php?jsoncallback=success_jsonpCallback", //调用自定义函数
        success:function(msg){
            alert(msg);
        },
        error:function(msg){
            alert(123);
            alert(msg);
        }
    });


    function success_jsonpCallback (result) {
        alert(result.img_url);
    }
</code>

PHP文件(test.php)

<code>$data['status'] = "1";
$data['img_url'] = "http://www.baidu.com/img/baidu_jgylogo3.gif";
$jsoncallback = $_GET['jsoncallback'];
//var_dump($_GET['jsoncallback']);
echo $jsoncallback."(".json_encode($data).")";
</code>

此外 希望有前端大神能给予解答.

error handler是Ajax Event。

This handler is not called for cross-domain script and cross-domain JSONP requests.

建议题主使用jsonp,jsonpCallback参数。

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