Hi,
I'm aiming to build up a JSON array of mouse positions and pass them to a controller. For some reason my json is returning from the controller as undefined can anyone spot my problem?
// 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);
}
});
I then receive the jsontext from my .NET MVC Controller:
public JsonResult Save(string personResults)
{
return Json(personResults);
}
As you can see the response back to the AJAX should just be the same json I sent to it - but I'm receiving undefined values back from the server even though my json seems to be constructing Ok and I've tested it - it's valid.
If I set Save to receive of type "string" I receive this alert "(new String(""))"; if i set the save action to receive of type "JsonResult" I receive this alert:
({ContentEncoding:null, ContentType:null, Data:null, JsonRequestBehavior:1})
Am I missing something totally obvious? I just want to check that my json is being sent successfully to the controller so I can manipulate it later!
Here's the format of my 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}
]}
Thanks!