【问题标题】:Phonegap AJAX call failsPhonegap AJAX 调用失败
【发布时间】:2013-04-09 06:14:44
【问题描述】:

我正在尝试获取一个简单的 phonegap 应用程序来与我的服务器通信。

这是我的javascript:

<script src="js/jquery.min.js" ></script>
    <script>
        $.ajax({
            url: 'http://www.drimmit.com/test',
            dataType: 'jsonp',
            jsonp: 'jsoncallback',
            timeout: 5000,
            success: function(data, status){
                //data loaded
                alert(data);
            },
            error: function(){
                //error loading data
                alert('no data');
            }
        });
    </script>

我还在 config.xml 中将域列入白名单:

<access origin="http://www.drimmit.com" subdomains="true"/>

在我的服务器上,我正在做一个简单的 echo 语句。

<?php
header('Content-type: application/json');
echo 'hello from server';

?>

最后,我只是得到一个弹出窗口(iOS)说“没有数据”,这意味着它失败了。

【问题讨论】:

    标签: jquery ios ajax cordova


    【解决方案1】:

    您已经提到请求类型为 jsonp,但您正在从服务器渲染 json,因此问题是,如果您从 ajax 调用中删除“jsonp:'jsoncallback'”语句,它应该可以正常工作,也替换 dataType: 'jsonp' 与 dataType:'json'。

    PS - 如果您进行跨域请求,则使用 jsonp,但在 phonegap 中它不是必需的,因为您已经将域列入白名单。

    【讨论】:

    • 哦,我漏掉了datatype参数,改成json,把"dataType:'jsonp'"换成"dataType:'json'"
    【解决方案2】:

    您在发送字符串时提到了dataType: 'jsonp'

    <?php
    header('Content-type: application/json');
    echo json_encode('hello from server');
    ?>
    

    希望对你有帮助

    【讨论】:

    • 目前这不起作用。仍然没有数据响应:(
    • 错误:函数(XHR,textStatus,errorThrown){ alert("ERREUR:" + textStatus); alert("ERREUR:" + errorThrown); } 用这个替换错误函数,看看错误信息是什么,并在你的问题中添加
    • 我收到一个“ERREUR: parsererror”,然后是“ERREUR: Error: jQuery191023364612134173512_1365495173377 was not called”。感谢您迄今为止的帮助!
    猜你喜欢
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    相关资源
    最近更新 更多