I have a document containing valid JSON content. It's saved on my machine as filename.json
I have a web page using that uses and ajax post to call a method that will return a json object.
** Note, I probably dont need to Post, I'm not actually using the SearchObject or the Id I'm passing, this was cut an past from another page, just trying to get it to work before cleaning it up.
The method my ajax is calling:
[WebMethod]
[AllowAnonymous]
public JsonResult GetJson(SearchObject model)
{
string text = System.IO.File.ReadAllText(@"C:\Users\mroberts\Desktop\filename.json");
return Json(text);
}
This is working, it returns data in the following format:
[{"NAME":"xxx xxxx",
"DIRECT PHONE":"(xxx) xxx-xxxx",
"EMAIL":"xxxxxx@xxxxxxxxx.com",
"JOB TITLE":"xxxxxx",
"DEPARTMENT":"xxxxxx"}]
My Jquery/Ajax:
$(document).ready(function () {
// Add the page method call as an onclick handler for the div.
$("a#BigButton").click(function (e) {
e.preventDefault();
var a = $(this).attr("id");
var data = {
Id: a
}
$.ajax({
//url: "../Controllers/Home/GetTier",
url: "/Home/GetJson",
type: "POST",
data: data,
context: this,
error: function () { },
dataType: 'json',
success: function (response) {
console.log(response);
var loop = $(response).size();
for (var i = 0; i < loop + 1; i++) {
console.log(response);
}
}
});
});
});
However, when I return this, I am logging it to my console, and am getting an error: Error: Syntax error, unrecognized expression: my json result....
Im not sure why this is invalid. I validated I've reviewed my result, and used online validation to verify the structure of my json, and it appears to be fine.
Am I overlooking something?
Is it the "return Json" that is breaking things?