weixin_33676492 2016-05-07 14:19 采纳率: 0%
浏览 45

ASP.NET Ajax返回null

Tried reading other similar questions but can't quite detect what's wrong with mine. I'm attempting to build an ajax call as follows:

$.ajax({
    type: "POST",
    url: '@Url.Action("Search", "Map")', // Map Controller, Search Action
    data: JSON.stringify({ "Location": x }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        var table = "<table class='table'>";
        $.each(data, function (index, value) { // For each element in data:
            table += "<tr><td>" + value.title + "</td></tr>"; // add it to the table list
            var latlng = new google.maps.LatLng(value.latitude, value.longitude);
            var marker = new google.maps.Marker({
                position: latlng,
                map: map
            });

            gmarkers.push(marker);
        });
        table += "</table>";
        $("#myData").html(table);
    }
});

It's supposed to send the text acquired from:

$("#txtSearch").keyup(function () {
    var x = $("#txtSearch").val();

Now the text is acquired correctly, therefore 'x' does have a value when it's submitted. But once the ajax request above is processed the method being called which is the Search Action under the MapController is called with a null parameter:

[HttpPost]
public ActionResult Search (string title)
{
    Entities mapEntity = new Entities();
    string userId = User.Identity.GetUserId();
    var result = mapEntity.Markers.Where(x => x.title.StartsWith(title) && x.UserId == userId).ToList();
    return Json(result, JsonRequestBehavior.AllowGet);
}

As long as the title remains null I can't really do anything with it... Appreciate any help on resolving this issue.

  • 写回答

2条回答 默认 最新

  • weixin_33698823 2016-05-07 14:21
    关注

    Try replacing those 2 parameters of your AJAX request:

    data: JSON.stringify({ "Location": x }),
    contentType: "application/json; charset=utf-8",
    

    with this simple expression:

    data: { title: x },
    

    Notice that your action parameter is called title so that's what you should be sending.


    UPDATE:

    To resolve the 500 Internal Server error that you are getting, stop passing your domain entities to the views because they might contain circular references that cannot be JSON serialized. As always, use a view model to project your domain entities and include only the relevant information for the view:

    var viewModel = result
        .Select(x => new 
        { 
            Prop1 = x.SimpleProp1, 
            Prop2 = x.SimpleProp2,
            Prop3 = x.NestedProp.Prop3,
        })
        .ToList();
    
    return Json(viewModel, JsonRequestBehavior.AllowGet);
    

    Btw, if you had inspected the AJAX request in the network tab of your browser, you would have seen that the response from your server is an YSOD in which the precise cause for the error is written.

    Check it out so that you know how to debug those kind of problems next time (I have highlighted the important parts you should be looking at):

    enter image description here

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决