I am having a requirement to invoke an action returning Json data on another project from my MVC project (assuming cross origin), Using ajax. Ajax function
var url1 = 'http://localhost:55308/Reports?type=E&code=1200?callback=?";
$.ajax({
url: url1,
dataType: 'jsonp',
contentType : 'application/json; charset=UTF-8',
success: function (response) {
data = JSON.parse(response);
alert('sucess');
},
error: function(xhr,status, error) { alert('error' + error.message); }
});
It is invoking the controller but returning error :
Uncaught SyntaxError: Unexpected token :{"result1":"abc"}
Controller in another project is like below:
[AllowAnonymous]
public ActionResult Reports(int Code, string type,string callback=null){
--- processing data--(which is working fine)
generatedPath = "df";
StringBuilder sb = new StringBuilder();
JavaScriptSerializer js = new JavaScriptSerializer();
sb.Append(callback + "(");
sb.Append(js.Serialize(generatedPath));
sb.Append(");");
return Json(sb, JsonRequestBehavior.AllowGet);
}
Returning Json result. Should I encapsulate Json to JSONP data in ajax request. Stuck on this for two days and help on google is not helping. Please appreciate help. Please TIA
Edited : Added callback to jsonp request.Also dataserialized which gives me ({"df"}). Is this right?