首页 > 解决方案 > 我有错误 Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

问题描述

错误

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined (jquery.min.js:2)
at w.fn.init.val (jquery.min.js:2)
at Object.error (Default.aspx:380)
at u (jquery.min.js:2)
at Object.fireWith [as rejectWith] (jquery.min.js:2)
at k (jquery.min.js:2)
at XMLHttpRequest.<anonymous> (jquery.min.js:2)

你好,这是我的错误。我在网上搜索,但其他已解决的问题与我的代码不同。

我的代码:

$(".selectProjectElement").change(function () {
            $.ajax({
                method: "POST",
                url: "Default.aspx/affectProject",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                data: "{ id:" + $(this).parent().parent().attr("data-id") + ", elementCode:'" + $(this).val() + "', userID:'" + $("#hidden_userID").val() + "' }",
                success: function () {
                    alert("reussi");
                },
                error: function (xhr) {
                    alert("F" + $(this).val() + "F");
                    //alert("Une erreur est survenue.");
                    window.location.pathname = window.location.pathname;
                }
            });
        });

我不知道为什么我有这个错误并且没有找到解决方案。你有想法吗?提前致谢。

对不起我的英语不好。

标签: javascriptasp.netajax

解决方案


你的数据线错了。

它要么需要:

data: '{ "id": "' + $(this).parent().parent().attr("data-id") + '", "elementCode":"' + $(this).val() + '", "userID": "' + $("#hidden_userID").val() + '"}',

或者:

data: { "id": $(this).parent().parent().attr("data-id"), "elementCode": $(this).val(), "userID": $("#hidden_userID").val()},

就我个人而言,我会用外面的变量来做

$(".selectProjectElement").change(function () {
  var data = { 
    id: $(this).parent().parent().attr("data-id"),
    elementCode: $(this).val(),
    userID: $("#hidden_userID").val()
  }
  $.ajax({
    ...
    data: JSON.stringify(data),
    ...
  });
});

推荐阅读