2010-10-01 118 views
0

我打算構建一個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} 
]} 

謝謝!

回答

1

我如何理解你的問題,你想知道如何將數據(輸入參數)發送到你的MVC控制器的作用,這將返回JsonResult回來。

您的第一個錯誤是,動作Save的輸入數據應該是而不是JSON格式。 MVC中設計了更多的表單,所以如果你發送任何數據到Save行動的數據必須是url編碼。例如有「喇嘛喇嘛」作爲行動的personResults參數的輸入Save您的jQuery調用應該是繼

$.ajax({ 
    url: "Home/Save", 
    type: "POST", 
    data: "personResults=Bla+Bla", 
    success: function (data) { 
     alert(data); }, 
    error: function (req, status, error) { 
     alert("error! " + status + error); 
    } 
}); 

更多最好不要使字符串「喇嘛喇嘛」的任何手動編碼和使用data: {personResults: "Bla Bla"},而不是data: "personResults=Bla+Bla"

如果你想發送更復雜的數據到MVC控制器,你可以這樣做,但你必須將數據轉換爲JSON並手動返回。例如,如果你有一個新動作Save1:(以同樣的方式,你可以使用DataContractJsonSerializer代替JavaScriptSerializer

public JsonResult Save1 (string encodedAnswers) { 
    JavaScriptSerializer serializer = new JavaScriptSerializer(); 
    MyData data = serializer.Deserialize<MyData> (encodedAnswers); 
    return Json (data); 
} 

其中MyData定義爲

public class MyData 
{ 
    public List<MyPosition> answers { get; set; } 
} 
public class MyPosition 
{ 
    public int xpos { get; set; } 
    public int ypos { get; set; } 
} 

相應的JavaScript代碼可以以下

var personResults = {"answers":[{"xpos":293, "ypos":415 },{"xpos":293, "ypos":416}]}; 
// or var personResults = {answers: [{xpos: 293, ypos: 415},{xpos: 293, ypos: 416}]}; 

$.ajax({ 
    url: "Home/Save1", 
    type: "POST", 
    data: {encodedAnswers: JSON.stringify(personResults)}, 
    success: function (data) { 
     alert("answers[0].xpos="+data.answers[0].xpos); }, 
    error: function (req, status, error) { 
     alert("error! " + status + error); 
    } 
}); 
+0

感謝您的深入響應,這超出了我的希望!我基本上試圖看看我的Json是否有效,因爲我需要將它映射到上面顯示的數據類。所以現在我將字符串反序列化爲一個類對象,並循環該對象來對這些值執行操作!歡呼=] – Sykth 2010-10-04 10:19:58

+0

感謝所有的迴應以及你的時間感謝! – Sykth 2010-10-04 10:20:57

+0

@Sykth:您好! – Oleg 2010-10-04 10:27:03

1

我真的不明白你的控制器操作的有效性,但因爲它的輸入接收您可以使用Content方法,如果您只是希望它返回相同的JSON字符串:

public ActionResult Save(string personResults) 
{ 
    return Content(personResults, "application/json"); 
} 
0

你$。 Ajax調用將數據轉換爲字符串,並且只返回一個字符串。它可能無法弄清楚如何處理這個問題,也可能沒有必要。

不過,建議你試試這個在您的控制器:

return Json(new { personResults = personResults });