当前位置: 首页 > 工具软件 > form-binder > 使用案例 >

ajax modelbinder,jquery - WebAPI - Ajax POST Json call (Cross-domain) - Model-binder not working - S...

艾焕
2023-12-01

On the Client-side:

var data = {Title":"Some Title","Description":"Something"} ;

$.support.cors = true;

$.ajax({

type: 'POST',

dataType: 'json',

url: apiUrl + "/Save",

data: data,

contentType: 'application/x-www-form-urlencoded',

success: function (returnedData) {},

error: function (xhr, ajaxOptions, thrownError) {},

processData: false,

async: false

});

On the Server-side:

[HttpPost]

public ActionResult Save(DataModel data)

{

Response.AppendHeader("Access-Control-Allow-Origin", "*");

Response.AppendHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

return null;

}

This is the DataModel:

public class DataModel

{

public string Title { get; set; }

public string Description { get; set; }

}

Everything works correctly. However, when it makes a POST request to the "Save" controller-action, the properties on the Model is always null and not getting the values from the client-side.

Note that this is a cross-domain request to the "sub-domain" of my site (i.e. calling from example.com to api.example.com)

What I'm missing?

Thanks all in advance!

 类似资料: