简体   繁体   English

jQuery ajax无法在iOS中运行

[英]jQuery ajax not working in iOS

One of our engineers just brought his iPad over to me and showed a feature not working on our site. 我们的一位工程师刚把他的iPad带到我面前,并展示了一个不在我们网站上工作的功能。 This works in Chrome and Firefox, but it doesn't work on the iPhone or iPad. 这适用于Chrome和Firefox,但它无法在iPhone或iPad上运行。 It's 3 select boxes that when you click the value in the first one, it runs ajax and populates the 2nd select box. 这是3个选择框,当您单击第一个中的值时,它会运行ajax并填充第二个选择框。

This is the functionality that doesn't work in iOS. 这是在iOS中不起作用的功能。

We're a bit stumped on where to begin testing this. 我们有点难以开始测试这个。 Can anyone provide some advice on where to begin debugging this or can you see anything we did wrong? 任何人都可以提供一些建议,从哪里开始调试这个或者你能看到我们做错了吗?

    $().ready(function () {
        $('.vehicle-search .make').bind('click', function (e) {
            var makeId = $(e.target).val();
            var container = $(e.target).parent('.vehicle-search');
            if (parseInt(makeId) > 0) {
                $.ajax({
                    url:  site.internal.url + '/lib/ajax/vehicle/make/getModelList.php',
                    type: 'post',
                    dataType: 'json',
                    success: function (r) {
                        if (r.length > 0) {
                            $('.vehicle-search > .model').html('');
                            $('.vehicle-search > .year').html('');

                            var html = '';
                            for (var i = 0; i < r.length; i++) {
                                html += "<option value='"+r[i].id+"'>"+r[i].name+"</option>";
                            }

                            $('.vehicle-search > .model').html(html);
                        } else {
                            alert('We did not find any models for this make');
                        }
                    },
                    error: function () {
                        alert('Unable to process your request, ajax file not found');
                        return false;
                    },
                    data: {
                        makeId: makeId
                        }
                });
            } else {
                $('.vehicle-search > .model').html('');
                $('.vehicle-search > .year').html('');
            }
        });
........

Bind to the change event of the <select> instead of the click event. 绑定到<select>change事件而不是click事件。

Also, you should enable the Developer Console for the iPad so that you can see any JS errors that may or may not be occurring. 此外,您应该启用 iPad 的开发者控制台 ,以便您可以看到可能发生或可能不发生的任何JS错误。

In the future, put in console.log statements in event handlers as a sanity check to see if they are getting called at all, and if they are to determine at what point they are failing. 在将来,将事件处理程序中的console.log语句作为完整性检查放入,以查看它们是否正在被调用,以及它们是否要确定它们在何时失败。

I have no idea what's wrong, but generally speaking, here is what you can do with Mobile Safari and IE that comes with no handly debug console. 我不知道出了什么问题,但总的来说,这就是你可以用Mobile Safari和IE做的,没有手动调试控制台。

  1. Add some alert() to your code to at least find out when did the script stop. 在代码中添加一些alert()以至少找出脚本何时停止。
  2. you event handlers doesn't receive any variable except r in success(r) . 除了r success(r)之外,事件处理程序不会收到任何变量。 Look into the doc, you will find variables that you could probe or alert() that could help you what's really going on. 查看文档,您将找到可以探测或alert()变量alert() ,它们可以帮助您实现真正的目标。 Especially the error() handler. 特别是error()处理程序。
  3. compete() handler will execute whenever the ajax had succeed of failed. 只要ajax成功失败,competition compete()处理程序就会执行。 Try to catch the variables too. 尝试捕捉变量。

The problem is the cache. 问题是缓存。 Add this line of code and the problem should be solved. 添加这行代码,问题应该解决。

$.ajaxSetup({ cache: false });

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

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