【发布时间】:2013-06-11 08:25:44
【问题描述】:
我有一个测试网页,用于调用 WebApi URL 并获得回复。它在 IE 中按预期工作,但在其他浏览器中没有。
这是我做的 jQuery 调用
$(document).ready(function()
{
jQuery("#cmdCallServiceWithParameter").on("click", function(event)
{
$.ajax({
type: "GET",
url: "http://localhost:64804/api/Voucher/5",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function ()
{
alert("success");
},
error: function (jqXHR, textStatus, errorThrown)
{ // Debug the error!!
alert("error " + textStatus);
alert("error throw " + errorThrown);
alert("incoming Text " + jqXHR.responseText);
}
})
})
});
这是凭证控制器中的代码
// GET api/voucher/5
public string Get(int id)
{
//return JsonConvert.SerializeObject(id, typeof(int), Formatting.Indented, new JsonSerializerSettings());
return "{\"test\":" + id.ToString() + "}";
}
IE 返回成功消息,但对于其他三个浏览器,我在显示的错误函数中收到三个警报。唯一要设置的参数是textStatus,设置为'error'
当我在 Firefox 和 Chrome 上直接浏览到 URL (http://localhost:64804/api/Voucher/5) 时,它会在浏览器窗口中显示 "{\"test\":5}"。 IE 要求我下载一个名为 5.json 的文件,而 Opera 要求我下载一个名为 5 的文件。这两个文件都只包含上面显示的文本,显示在 Firefox 和 Chrome 的窗口中
有没有人知道这可能是什么问题,以及如何让它在所有浏览器中工作?
【问题讨论】:
-
可能是跨域问题,您从哪个 url 向 api 发出请求?
-
我调用 jQuery 的测试网页位于
http://localhost:65513/Home.html。 -
那么它的来源不同。尝试数据类型:“json”
标签: jquery ajax asp.net-web-api