Asp.Net Mvc JQuery ajax input parameters are null

后端 未结 2 903
栀梦
栀梦 2020-12-18 04:38

I am trying to post some data with jQuery Ajax, but the parameters in my Ajax method are null.

This is simple test to send data:

 var dataPost = { ti         


        
相关标签:
2条回答
  • 2020-12-18 05:10

    We don't need the contentType: 'application/json; charset=utf-8', and $.toJSON

    Here is the code makes me happy!

     $(function () {
            $("#btnSumbit").click(function () {
                $('#results').hide();
                $('#loadingmessage').show();
                var a = $("#query").val();
    
                $.ajax({
                    type: "POST",
                    url: "/Search/Index",
                    data: ({ query: a }),
                    datatype: "json",
                    success: function (data) {
                        $('#results').empty();
                        for (var i = 0; i < data.length; i++) {
                            var div = "<div>" + data[i].Name + "</div>";
                            $("#results").append(div);
    
                        }
                        $('#loadingmessage').hide();
                        $('#results').show();
                    },
                    failure: function (errMsg) {
                        $('#loadingmessage').hide();
                        alert(errMsg);
                    }
                });
            });
        });
    
    0 讨论(0)
  • 2020-12-18 05:18

    The Create controller action doesn't expect parameters to be JSON serialized so you don't have to. Try passing them directly instead:

    var dataPost = { titel: 'titel', message: 'msg', tagIds: 'hello' };
    jQuery.ajax({
        type: "POST",
        url: "Create",
        data: dataPost,
        dataType: "json",
        success: function(result) {
            alert("Data Returned: ");
        }
    });
    
    0 讨论(0)
提交回复
热议问题