JQuery的:“未捕获的类型错误:非法调用”在Ajax请求时,数据参数数组 [英] JQuery: 'Uncaught TypeError: Illegal invocation' at ajax request when data parameter is array

查看:2636
本文介绍了JQuery的:“未捕获的类型错误:非法调用”在Ajax请求时,数据参数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个选择元素,A和B:当A公司选择的选项变化,B的选项必须进行相应的更新。 A中的每个元素意味着B中的许多元素,它是一个一对多的关系(包括国家,乙方应包含设在特定国家的城市)。

I have two select elements, A and B: when A's selected option changes, B's options must be updated accordingly. Each element in A implies many elements in B, it's a one-to-many relationship (A contains nations, B should contain cities located in the given nation).

功能 do_ajax 应该运行的异步请求:

The function do_ajax should run the asynchronous request:

function do_ajax(elem, mydata, filename)
{
    $.ajax({
        url: filename,
        context: elem,
        data: mydata,
        datatype: "html",
        success: function (data, textStatus, xhr) {
            elem.innerHTML = data;
        }
    });
}

为了更新B的选项我添加在A的的onChange 事件函数调用。下面是运行时的功能onChange事件(的 A )被触发:

In order to update B's options I've added a function call in A's onChange event. Here is the function that runs when onChange event (of A) is triggered:

function my_onchange(e) // "e" is element "A"
{
    var sel_B = ... ; // get select element "B"

    // I skipped some code here
    // ...

    var data = {
        'mode': 'filter_city',
        'id_A': e[e.selectedIndex]
    };
    do_ajax(city_sel, data, 'ajax_handler.php');
}

}

我读过的 JQuery的文档数据可以是一个数组(键值对)。我得到的错误,如果我把:

I've read in JQuery docs that data can be an array (key value pairs). I get the error if I put:

var data = {
        'mode': 'filter_city',
        'id_A': e[e.selectedIndex]
};

相反,我没有得到这个错误,如果我的数据是一个字符串:

Instead, I don't get that error if my data is a string:

var data = 'mode=filter_city&id_A=' + e[e.selectedIndex];

但我需要的数组版的变量,在我的服务器端PHP code。

But I need the "array version" of the variable, in my server-side php code.

未捕获的类型错误:非法调用位于的jQuery 1.7.2.min.js的文件,这是所有的COM pressed,所以我想不出什么code部分提出的错误。

The Uncaught TypeError: Illegal invocation is located in the "jquery-1.7.2.min.js" file, which is all compressed, so I couldn't figure out what part of code raised the error.

有没有设置,我可以在我的code改变,因此它接受的数据作为关联数组?

Is there any setting I can change in my code so that it accepts data as an associative array?

推荐答案

由于与对话 Sarfraz 我们可以找出解决方案。

Thanks to the talk with Sarfraz we could figure out the solution.

但问题是,我经过它的价值,这实际上正是我想做的事(其实在我的PHP code,我需要这个值作为外键查询我的<$ C的一个HTML元素,而不是$ C>城市表和筛选正确项)。

The problem was that I was passing an HTML element instead of its value, which is actually what I wanted to do (in fact in my php code I need that value as a foreign key for querying my cities table and filter correct entries).

所以,相反的:

var data = {
        'mode': 'filter_city',
        'id_A': e[e.selectedIndex]
};

应该是:

var data = {
        'mode': 'filter_city',
        'id_A': e[e.selectedIndex].value
};

注意:检查贾森Kulatunga的回答,它引用JQuery的文档解释为什么传递一个HTML元素是造成麻烦。

Note: check Jason Kulatunga's answer, it quotes JQuery doc to explain why passing an HTML element was causing troubles.

这篇关于JQuery的:“未捕获的类型错误:非法调用”在Ajax请求时,数据参数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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