【问题标题】:Strange error: Uncaught TypeError: Illegal invocation ajax奇怪的错误:未捕获的TypeError:非法调用ajax
【发布时间】:2015-08-13 05:53:59
【问题描述】:

更新

问题解决了,看来我是傻了,引用title元素后忘记放.value了。


我已经使用 ajax 几个月了,之前从未遇到过这样的错误。

我正在创建一个 formData 数组并使用 ajax 将其传递给一个 php 脚本,就像我以前做过很多次一样,但是我现在遇到了那个奇怪的错误。

这可能与我的 formData 有关吗?我看不出有什么不同,因为它与我以前的做法没有什么不同。

任何帮助都会很棒,谢谢!

    //This function will add the product and its details to the cart.
    function addToCart(e)
    {
        //alert("CLick");
        calculatePrice();

        //Get the product ID from the URL
        var id = getParameterByName('productID');
        alert(id);
        //Get the title.
        var title = document.getElementById('title');
        //Get the qty.
        var qty = document.getElementById('qty').value;         
        //Get the weight.
        var weightList = document.getElementById('weightList');
        var weightText = weightList.options[weightList.selectedIndex].text;
            //alert("Text: " + weightText);
        var weightValue = weightList.options[weightList.selectedIndex].value;
            //alert("Val: " + weightValue);

        formData = {"ID" : id, "SRC": "", "Title" : title, "Qty" : qty, "Weight" : weightText, "Totalprice" : totalPrice, "Singularprice" : weightValue, "Action" : "Add"};
        $.ajax(
                {           
                url: 'manipulateCart.php',
                type: "POST",
                data: formData,
                success: function(data)
                {       
                    alert("cc");
                    //document.getElementById('content').innerHTML = "";
                    //$('#content').append(data);   
                },          
                error: function(xhr,err)
                {       
                    alert("Error: " + xhr + " " + err);
                }

            });

    }

【问题讨论】:

  • weightTextweightValue 他们会完美吗?
  • 嗨,weightText 返回如下:110g Bag 和 weightValue 返回如下:3.00。会不会和 weightValue 上的 double 值类型有关?

标签: javascript php ajax


【解决方案1】:

这一行缺少.value

 var title = document.getElementById('title').value;
                                              ^^^^^

【讨论】:

  • 啊,是的,这就是原因,谢谢!我一定是在赶那条线,忘了放它。再次感谢,+1!
【解决方案2】:

您正在将 title 变量传递给 formData 对象。此变量保存#title 元素,而不是该元素的值。将.value添加到var title = document.getElementById('title');

结果:

var title = document.getElementById('title').value;.

更多信息:https://stackoverflow.com/a/11071290/1130234

【讨论】:

    猜你喜欢
    • 2011-05-15
    • 2019-09-21
    • 2017-06-16
    • 1970-01-01
    • 2013-07-31
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多