jQuery的阿贾克斯后不工作在iPhone和Android [英] Jquery ajax post not working on iPhone and Android

查看:155
本文介绍了jQuery的阿贾克斯后不工作在iPhone和Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个Web应用程序针对移动设备(特别是iPhone和Android)。我建立它使用ASP.NET MVC和jQuery。在一个页面上我有一个级联下拉。我已经迷上了一个功能上的改变事件的第一个下拉,并且不AJAX调用,并使用JSON响应来填充第二个下拉。这一切工作正常我的桌面上,但不会从iPhone或Android手机的工作。不填充第二个下拉列表中。我在更改功能设置的警告,并证实该事件被触发。然后我把警报,在回调函数AJAX调用,并没有得到执行,因此无论是AJAX调用不打服务器,或者未收到电话的响应。

I'm building a web application targeting mobile devices (specifically iPhone and Android). I'm building it using ASP.NET MVC and JQuery. On one page I have a cascading dropdown. I've hooked up a function on the change event for the first dropdown and that does an AJAX call and uses the JSON response to populate the second dropdown. This all works fine on my desktop, but does not work from an iPhone or Android phone. The second dropdown is not populated. I placed an "alert" in the change function and it verified that the event is being triggered. I then placed the "alert" in the callback function for the AJAX call and it didn't get executed, so either the AJAX call is not hitting the server, or the response is not being received by the phone.

jQuery的code是:

The jQuery code is:

    $(function () {
    $("#ProgramId").change(function () {
        var programId = $("#ProgramId").attr("value");
        alert("Change event triggered"); // <- test only, does get executed when client is a phone bowser
        var termId = $("#TermId").attr("value");
        $.post("/DealerHome/Terms", { "ProgramId": programId },
            function (data) {
                alert("Ajax response received"); // <- test only, does NOT get executed when client is a phone bowser
                $("#TermId").children().remove();
                $("#TermId").append('<option value="0">--select--</option>');
                $.each(data, function () {
                    if (termId == this.Value) {
                        $("#TermId").append('<option value="' + this.Value + '" selected >' + this.Text + '</option>');
                    } else {
                        $("#TermId").append('<option value="' + this.Value + '" >' + this.Text + '</option>');
                    }
                });
            });
    });
});

我打开iPhone上的JavaScript控制台,但它并没有显示出任何错误。作为新开发的移动设备,我不知道我应该如何调试这一点。在桌面上,我只想用小提琴手,看看发生了什么事在电线上。任何帮助是非常AP preciatec。

I've turned on JavaScript Console on the iPhone, but it doesn't show any errors. Being new to developing for mobile devices, I'm not sure how I should debug this. On the desktop, I would just use Fiddler to see what's happening on the wire. Any help is much appreciatec.

干杯

克雷格

推荐答案

我已经解决了这个问题,我只是张贴在其他情况下遇到了同样的问题。它归结为一个我做的非常的事情我恨,那是很难codeD字符串。在这种情况下,违规行为AJAX网址兴田后的功能。很难coded到网站的根,但是当部署了(这是我是如何在移动设备上测试它)的路径改变了。

I've solved the issue and I'm just posting it in case others run into the same problem. It comes down to a me doing the very thing I hate, and that is hardcoded strings. In the case of this, the offending line is the AJAX url in hte post function. It's hardcoded to the root of the website, but when deployed (which was how I was testing it on a mobile device) the path changed.

SO纠正这一点,我把它换成以上后行:

SO to correct this, I've replaced the post line above with:

$.post('@Url.Content("~/DealerHome/Terms")' , { "ProgramId": programId },

Url.Content确保正确的相对路径用于链接

Url.Content ensures that the correct relative path is used for the url.

这篇关于jQuery的阿贾克斯后不工作在iPhone和Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆