我有这段代码应该从本地主机站点获得 JSON 答案。如果我从 Chrome 浏览此 URL,我会得到很好的响应,但如果我尝试从 HTML 页面运行此代码,我会收到错误(通过 VS 调试)。
有任何想法吗?
$(document).ready(function () {
alert("hello");
var request = $.ajax({
url: "http://localhost:3157/api/Products",
type: "GET",
dataType: "json"
});
request.done(function(msg) {
alert("msg is: " + msg);
});
request.fail(function (jqXhr, textStatus) {
alert("Request failed: " + textStatus);
});
});
更新 - 添加了 xml 输出
<ArrayOfProduct xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/FirstWebApi.Models">
<Product>
<Category>Groceries</Category>
<Id>1</Id>
<Name>Tomato Soup</Name>
<Price>1</Price>
</Product>
<Product>
<Category>Toys</Category>
<Id>2</Id>
<Name>Yo-yo</Name>
<Price>3.75</Price>
</Product>
<Product>
<Category>Hardware</Category>
<Id>3</Id>
<Name>Hammer</Name>
<Price>16.99</Price>
</Product>
</ArrayOfProduct>