2014-10-02 153 views
0

我试了很多,所以优化代码,但ajax将无法正常工作。我在ajax之前用一些调试警报测试了js,它工作正常!所以只有我的ajax不能在用phonegap创建的Android apk中工作。Phonegap .ajax不工作

document.addEventListener("deviceready", function(){ 
       var user_id = localStorage.getItem('user_id'); 
       alert(user_id); //this works fine 
       $.ajax({ 
        type: "GET", 
        url: "http://marcelkipp.com/quizapp/userinfo.php?user_id="+user_id, 
        async: false, 
        dataType: "json", 
        success: function(data) { 
         alert(data.username); //this do not work 
         setTimeout(function(){window.location.href="menu.html";}, 3000); 
        }, 
        error: function(){ 
         alert("FAIL"); 
        } 
       }); 
     }); 
+0

你的代码不工作的方式是什么? – APerson 2014-10-02 13:19:24

+0

我怀疑你遇到了跨域问题,如果你在应用程序中运行这个问题......你必须在你的服务器上启用跨域访问。 http://stackoverflow.com/questions/24043136/phonegap-cross-domain-ajax-with-phonegap-developer-app-and-cli – 2014-10-02 13:29:02

+0

警报成功和错误内的功能不起作用 – 2014-10-02 13:29:43

回答

0

好的。现在我知道为什么我的ajax失败了。 我从谷歌库中导入了jquery,我将jquery-file集成到了我的www文件夹中,并且工作正常。