0

我正在尝试在 Android 模拟器上使用 jsonp 调用服务:

    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },

    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },

    receivedEvent: function(id) {
        console.log('Received Event: ' + id);

        $.ajaxSetup ({  
            cache: false  
        });

        $.ajax({
            type: "GET",
            url: "http://10.0.2.2/uk-en/login/user",
            dataType: "jsonp",
            contentType: "application/json",
            success: function(xml){
                $("#result").html('123');
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) { 
                $("#result").html('XMLHttpRequest = ' + JSON.stringify(XMLHttpRequest) + ", textStatus = " + textStatus + ', errorThrown = ' + JSON.stringify(errorThrown));
            } 
        });
    }

我在 HelloWorld 应用程序上执行此操作,在 phonegap 网站上创建以下文档。我index.html的类似于这样:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <title>Hello World</title>
</head>
<body>
    <div class="app">
        <div id="result">

        </div>
    </div>
    <script type="text/javascript" src="cordova-2.2.0.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
</body>

这是我收到的错误,尽管这适用于我本地机器上的 Firefox:

XMLHttpRequest = {"READYSTATE":4,"STATUSTEXT":"SUCCESS"},TEXTSTATUS = PARSERERROR, ERRORTHROWN = {"MESSAGE":JQUERY1830456564654_32131 WAS NOT CALLED", "STACK":"ERROR:JQUERY1830456564654_32131 WAS NOT CALLED"\N AT FUNCTION.ERROR(http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js:2:13215)

请问有人可以帮忙吗?

4

1 回答 1

0

JSONP要求将响应包装在某种回调函数中。

这一切都假设您从另一个域获取内容。如果是这样,您将受到同源策略的限制:

所以服务器应该响应:

urFunction({....});
于 2013-01-11T12:26:36.177 回答