简体   繁体   English

在Phonegap模拟器/ iPhone中解析JSON与通过浏览器进行解析

[英]Parsing JSON in Phonegap simulator/iPhone vs. through browser

I am working on an iPhone app using Phonegap with just JS/HTML5 on the front-end and .NET on the back-end. 我正在使用仅在前端使用JS / HTML5在后端使用.NET的Phonegap开发iPhone应用程序。 For the login I am just making a call to the web service and get JSON back. 对于登录,我只是调用Web服务并返回JSON。 The problem I am having is when debugging this in the desktop browser vs. debugging it on the actual phone/simulator. 我遇到的问题是在台式机浏览器中调试此产品而不是在实际的电话/模拟器中调试它。

The JSON that is retrieved looks like this when there is a valid login: 有效登录后,检索到的JSON如下所示:

{"AuthToken":null,"Errors":[],"Success":true,"Message":null,"IsValid":true}

So I have this Ajax success function (jQuery): 所以我有这个Ajax success函数(jQuery):

success: function (data) {
    //this works on phone, but not in browser
    var success = data.Success;  //returns true             
    //this works in browser, but errors out on phone
    var dta = JSON.parse(data);
   success = dta.Success //returns true;
}

Any idea why this might happen or how I can correct it? 知道为什么会发生这种情况或如何纠正吗? It's just easier for me to debug in the browser using Firebug instead of always using the simulator. 对于我来说,使用Firebug在浏览器中进行调试要容易得多,而不必总是使用模拟器。 I have been using some other debug methods (Weinre for one) but none are as good as using Firebug. 我一直在使用其他一些调试方法(Weinre就是其中一种),但是没有一种方法比使用Firebug更好。

I had the same problem yesterday, went smoothly on the iPhone but not on the web, annoying. 昨天我遇到了同样的问题,在iPhone上运行流畅,但在网络上却运行不畅,这很烦人。

After some searching I found that, if you set the dataType: 'jsonp' , and set a callback function jsonp: 'jsoncallback' 经过一些搜索,我发现,如果您设置dataType: 'jsonp' ,并设置了回调函数jsonp: 'jsoncallback'

My handler looks something like this: 我的处理程序看起来像这样:

$('form').on('submit', function(event){
        var myData = $(this).serialize();
        jQuery.ajax({
            url: 'www.mysite.com/submit.php',
            type: 'POST',
            data: myData,
            dataType: 'jsonp',
            jsonp: 'jsoncallback',
            success: function(data, textStatus, jqXHR){
                alert('success: ' + data)
                },
            error:  function(jqXHR, textStatus, errorThrown){
                alert('error')
                }
            });
        return false;
        })

On the server side I end off with: 在服务器端,我以:

echo $_GET['jsoncallback'] . '(' . json_encode($myData) . ');'

Something about the same origin policy since your local directory is not in the same domain as your server. 由于您的本地目录与服务器不在同一个域中,因此具有相同的原始策略 Apparently jsonp (p for padded) can work cross domain, I'll post the article explaining this as soon as I find it. 显然jsonp(p用于填充)可以跨域工作,我将在发现它后立即发布解释此问题的文章。

UPDATE: Here's where I got the tipoff maybe you've googled it already 更新: 这是我给小费的地方,也许您已经用谷歌搜索了

Wish I could give you the .NET code but I hope this helps 希望我能给您.NET代码,但希望对您有所帮助

Simple little thing.. 简单的小事..

needed to add dataType: "json" for a parameter on the call for it to work on the web side. 需要在调用上添加dataType:“ json”作为参数,以使其在Web端起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM