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

javascript - jQuery ajax跨域问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:42:50
Original
1048 people have browsed it

遇到如下情况 使用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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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

回复内容:

遇到如下情况 使用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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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

error handler是Ajax Event。

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

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

Related labels:
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template