MVC3 REST Deployment
I've created a basic MVC REST API to just get single Person from the datasource. It all works locally but not when I deploy it on to the server.
public JsonResult Get(int? id)
{
if (id != null)
{
Person p = personBl.Get((int)id);
return Json(p, JsonRequestBehavior.AllowGet);
}
return Json("");
}
My jquery call is as follows:
$.ajax({
type: 'GET',
url: 'http://localhost:50708/Persons/Get/',
data: { id: 20 },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.Name);
}
});
When开发者_开发技巧 I deploy it on to my windows 2003 server and call the Get function, it's always 404 (obviously there's no View attached as I just want the json).
I use a jsonp call as the server is on a different domain.
$.ajax({
type: 'GET',
url: 'http://192.168.1.187:1500/Persons/Get/',
data: { id: 20 },
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
jsonpCallback: 'jsonpCallback'
});
function jsonpCallback(data) {
alert('callback');
}
Any idea why the above doesn't seem to work?
I don't see how you are using JSONP. Your server still returns plain JSON:
return Json(p, JsonRequestBehavior.AllowGet);
which will be sent like this over the wire:
{ firstName: 'John', lastName: 'Smith' }
A JSONP response would look like this:
callbackname({ firstName: 'John', lastName: 'Smith' })
It is not enough to only instruct the client about jsonp
=> the server needs to support it.
You may take a look at the following blog post and use the JsonpResult
illustrated there on the server.
Is you're deployed site configured to use port 1500? Is it under a virtual directory? Somethings incorrect with http://192.168.1.187:1500/Persons/Get/
I would guess.
精彩评论