weixin_33724659 2015-11-23 14:47 采纳率: 0%
浏览 41

未定义的AJAX“数据”属性

I am trying to update some text within <span> fields on a parent form via a modal. To do this I am using an AJAX call on the modal form to retrieve the new user entered values. Once the "Save" button is clicked, the modal form should retrieve the textbox values and send them back.

However I am unable to pass any values back. When debugging in the function that creates a Javascript array, the values exist and are correct. However once I try to use the data property of the AJAX call (ex: data.Address), it returns undefined.

Here is my code for the AJAX call:

     $.ajax({
        type: 'POST',
        data:
            {
                ID: $("#clientID-@Model.Id").val(),
                Address1: $("#clientAddress1-@Model.Id").val(),
                Address2: $("#clientAddress2-@Model.Id").val(),
                City: $("#clientCity-@Model.Id").val(),
                Province: $("#clientProvince-@Model.Id").val(),
                PostalCode: $("#clientPostalCode-@Model.Id").val(),
                PhoneNumber: $("#clientPhoneNumber-@Model.Id").val()
            },
        success: function (data) {
            //$("#Address1-@Model.Id").html(data.Address1);

            alert(data); //prints the entire HTML for the form
            alert(data.ID); //prints undefined
            alert($("#clientID-@Model.Id").val()); //prints the correct value - 2827
            alert("Data logged!");
        },
        error: function (data) {
            alert("Error occurred!");
        }
    });
  • 写回答

1条回答 默认 最新

  • 斗士狗 2015-11-23 14:56
    关注

    If you are not sending the correct header from the server that say is json you must specified in the ajax object that the response is json try adding the dataType

     $.ajax({
        type: 'POST',
        dataType:'json',
        data: getModelData(),
        success: function (data) {
            //$("#Address1-@Model.Id").html(data.Address1);
            alert(data[0].Address1);
        },
        error: function (data) {
            alert("Error occurred!");
        }
    });
    

    Or in the server specified the content type application/json

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog