从onblur函数填充表单。 Javascript:JSON.parse返回undefined,尽管stringify返回值

时间:2017-01-29 02:18:06

标签: javascript json ajax asp.net-mvc

这是我的代码:

控制器:

public ActionResult GetCustomerData(string CustomerName)
{
    ClientBusinessLayer clientBusinessLayer = new ClientBusinessLayer();
    var clientId = clientBusinessLayer.GetClientId(User.Identity.GetUserId());
    List<CustomerWithPlans> customersWithPlans                                              clientBusinessLayer.GetCustomersWithPlans(clientId).ToList();

    //Searching records from list using LINQ query
    var Customer = (from N in customersWithPlans
                    where N.Customer.FullName.Equals(CustomerName)
                    select new { N.Customer });
    return Json(Customer, JsonRequestBehavior.AllowGet);
}

使用Javascript:

<script type="text/javascript">
    $(document).ready(function () {

        $("#Customer_FullName").blur(function () {
            alert("test: " + document.getElementById('Customer_FullName').value);
            $.ajax({
                url: "/Client/GetCustomerData",
                type: "POST",
                data: JSON.stringify({ 'CustomerName': document.getElementById("Customer_FullName").value }),
                dataType: "json",
                traditional: true,
                contentType: "application/json; charset=utf-8",
                success: function (jsondata) {
                    var data = JSON.stringify(jsondata, null, 2);
                    alert("Hello? " + data);
                    alert(jsondata[0].firstname);
                    alert(jsondata.firstname);
                },
                error: function () {
                    alert("An error has occured!!!");
                }
            });
        });
    });
</script>

如果有人可以帮我弄清楚如何将这些值添加到表单中,那将会有所帮助。

0 个答案:

没有答案