我打算構建一個JSON數組的鼠標位置並將它們傳遞給控制器。出於某種原因,我的JSON從控制器返回undefined任何人都可以發現我的問題?無法成功接收JSON數據到.NET MVC控制器
// Attempt at declaring a JSON object
var personResults = { "answers": [] };
// Onclick I fire in values from mouse event
personResults.answers[count] = { "xpos": mouseX, "ypos": mouseY };
// AJAX Call
$.ajax({
url: "Save",
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(personResults),
success: function (data) { alert(data.toSource()); },
error: function (req, status, error) {
alert("error! " + status + error + req);
}
});
然後我從我的.NET MVC控制器接收jsontext:
public JsonResult Save(string personResults)
{
return Json(personResults);
}
正如你所看到的響應返回給AJAX應該只是我發送給它的相同JSON - 但我米從服務器接收未定義的值,即使我的JSON似乎在構建好,我已經測試了它 - 它是有效的。
如果我設置保存以接收類型「字符串」,我收到此警報「(new String(」「))」;如果我設置保存操作即可接收類型「JsonResult」我收到此警報:
({ContentEncoding:null, ContentType:null, Data:null, JsonRequestBehavior:1})
我缺少的東西完全明顯?我只想檢查一下,我的json是否已經成功發送到控制器,以便稍後操作它!
這裏是我的JSON格式:
{"answers":[
{"xpos":293,"ypos":415},{"xpos":293,"ypos":415},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416}
]}
謝謝!
感謝您的深入響應,這超出了我的希望!我基本上試圖看看我的Json是否有效,因爲我需要將它映射到上面顯示的數據類。所以現在我將字符串反序列化爲一個類對象,並循環該對象來對這些值執行操作!歡呼=] – Sykth 2010-10-04 10:19:58
感謝所有的迴應以及你的時間感謝! – Sykth 2010-10-04 10:20:57
@Sykth:您好! – Oleg 2010-10-04 10:27:03