【问题标题】:jQueryMobile, PhoneGap getJSON method not executingjQueryMobile,PhoneGap getJSON 方法未执行
【发布时间】:2011-11-30 21:03:10
【问题描述】:

我正在运行一个在 PhoneGap 中运行的示例 jQueryMobile 应用程序。问题是它没有运行 getJSON 回调来检索数据,如下所示:

$( function() 

    {       
        $('#searchButton').click(function() 
        {

            alert("search clicked"); <== this alert works  

            var url = "http://api.alternativeto.net/software/firefox?callback=?";

            $.getJSON(url, function(data) <== this should be, but isn't being called
            {
                alert("function data called");  <== so this alert doesn't show.      

             } // end function (data) 

            ); // end getJSON

            alert("getJSON call completed");    <== and this alert works
        } //  end search click.function()
    ); // end $(

“搜索已点击”和“getJSON 调用已完成”警报都有效。我在浏览器中输入 url 得到的返回 JSON 是有效的。

测试代码来自这个网址:

http://wiki.phonegap.com/w/page/36868306/UI%20Development%20using%20jQueryMobile

还有什么我可以检查的吗?

【问题讨论】:

  • 我认为您不需要 url 中的 ?callback=?,因为您可以从应用程序发出跨域请求(我已经成功使用 $.getJSON('http://google.com/', function (data) {console.log(data);}))。
  • 谢谢 - 这帮助我查看日志而不是警报,在那里我发现存在某种白名单问题。我认为这已经解决了,所以如果你把它作为一个答案,我会接受它。

标签: jquery-mobile cordova


【解决方案1】:

我认为您不需要 ?callback=? 在您的 url 中,因为您可以从应用发出跨域请求。

我已经成功了:

$.getJSON('http://google.com/', function (data) {console.log(data);});

..它记录了 Google 主页的 HTML。

【讨论】:

  • @JackBeNimble 是的,我记得在 iOS 4.3 上测试过它并且它工作正常。但现在我想起来了,我使用了alert(data),因为我不知道如何让console.log(data) 工作(我对Xcode 还很陌生)。
  • 对不起 - 我不得不从接受更改为赞成 - 我已经摆脱了白名单问题,但它仍然没有执行回调。
  • 不,它正在工作 - 它一定是语法错误。谢谢!
  • 不工作它什么也不做,也不显示任何控制台日志
  • @ArpitGarg 如果您需要帮助,请随时询问,很可能您的其他设置不正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
  • 2012-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多