MVC3 REST 部署
我创建了一个基本的 MVC REST API 来从数据源获取单个 Person。这一切都在本地工作,但当我将其部署到服务器上时就不行了。
public JsonResult Get(int? id)
{
if (id != null)
{
Person p = personBl.Get((int)id);
return Json(p, JsonRequestBehavior.AllowGet);
}
return Json("");
}
我的 jquery 调用如下:
$.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);
}
});
当我将其部署到 Windows 2003 服务器上并调用 Get 函数时,它始终是 404(显然没有附加视图,因为我只想要 json)。
我使用 jsonp 调用,因为服务器位于不同的域上。
$.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');
}
知道为什么上面的方法似乎不起作用吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白你是如何使用 JSONP 的。您的服务器仍然返回纯 JSON:
它将像这样通过线路发送:
JSONP 响应将如下所示:
仅向客户端指示
jsonp
=> 是不够的。服务器需要支持它。您可以查看以下博客文章并使用
JsonpResult 说明在服务器上有。
I don't see how you are using JSONP. Your server still returns plain JSON:
which will be sent like this over the wire:
A JSONP response would look like this:
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.您部署的站点是否配置为使用端口 1500?是在虚拟目录下吗?我猜
http://192.168.1.187:1500/Persons/Get/
不正确。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.