gpt4 book ai didi

asp.net-mvc-3 - MVC3 REST 部署

转载 作者:行者123 更新时间:2023-12-02 02:25:49 24 4
gpt4 key购买 nike

我已经创建了一个基本的 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(显然没有附加 View ,因为我只想要 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');
}

知道为什么上面的方法似乎不起作用吗?

最佳答案

我不明白您是如何使用 JSONP 的。您的服务器仍然返回纯 JSON:

return Json(p, JsonRequestBehavior.AllowGet);

将像这样通过网络发送:

{ firstName: 'John', lastName: 'Smith' }

JSONP 响应如下所示:

callbackname({ firstName: 'John', lastName: 'Smith' })

仅向客户端指示jsonp => 服务器需要支持它是不够的。

你可以看看 following blog post并使用服务器上显示的 JsonpResult

关于asp.net-mvc-3 - MVC3 REST 部署,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6003185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com