jQuery posts null instead of JSON to ASP.NET Web API

后端 未结 5 755
南笙
南笙 2020-12-05 19:12

I can\'t seem to get this to work... I have some jQuery like this on the client:

$.ajax({
    type: \"POST\",
    url: \"api/report/reportexists/\",
    data         


        
5条回答
  •  醉酒成梦
    2020-12-05 20:12

    jQuery.ajax() by default sets the contentType to be application/x-www-form-urlencoded. You could send the request in application/json instead. Also, you should send your data as a string and it will get model bind to the report parameter for your post method:

    $.ajax({
        type: "POST",
        url: "api/report/reportexists/",
        contentType:  "application/json",
        data: JSON.stringify(reportpath),
        success: function(exists) {
            if (exists) {
                fileExists = true;
            } else {
                fileExists = false;
            }
        }
    });
    

提交回复
热议问题