下面的代码示例确实按预期运行,但对于我自己的启发,我想了解为什么我需要对位置对象进行字符串化。如果我没有对对象进行字符串化,那么控制器最终会超时。
function onClick(e) {
var g = new geoLoc2('geolocation', function (position) {
position.regId= window.localStorage.getItem("registrationId");
position.regType= "self";
position.playerId="1234567890";
console.log("this is position:" + JSON.stringify(position));
var stringy = JSON.stringify(position);
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "http://192.168.1.142/webapitest2/CheckIn",
data: stringy,
success: function (data) {
window.localStorage.setItem("actionToken", data.ActionToken);
},
error: function (error) {
jsonValue = jQuery.parseJSON(error.responseText);
console.log(jsonValue);
//jError('An error has occurred while saving the new part source: ' + jsonValue, { TimeShown: 3000 });
}
});
}).checkGPS();
}
这是控制器:
// POST: CheckIn
[HttpPost]
public JsonResult Index(CheckInViewModel json)
{
if (!ModelState.IsValid)
{
return Json(new { error = 'Invalid Model' });
}
return Json(new { Success= 'True' });
}
我还试图以字符串形式发布,然后将其转换为控制器中的对象,并具有相同的超时结果。
这里有一个类似的问题:Ajax POST call to ASP.NET