开发者

Jquery ajax post not working on iPhone and Android

开发者 https://www.devze.com 2023-02-22 21:42 出处:网络
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 funct

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.

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 res开发者_如何学Pythonponse 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>');
                    }
                });
            });
    });
});

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.

Cheers

Craig


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 to correct this, I've replaced the post line above with:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消