我试图使用get请求返回一系列产品.响应返回带有200请求的XML.
网络服务:
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public List<product> GetAllProducts()
{
using (SchulteDesignYourOwnEntities db = new SchulteDesignYourOwnEntities())
{
return db.products.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: 'http://www.organizeliving.com/designwebservice.asmx/GetAllProducts',
dataType: 'json',
success: function (result) {
alert("Result: " + result.length);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log("Status: " + xhr.status);
console.log("Message: " + thrownError);
}
});
});
</script>
</head>
<body></body>
</html>
Run Code Online (Sandbox Code Playgroud)
Exp*_*lls 14
你有dataTypeas 'json'.jQuery将自动尝试从响应中解析JSON.如果不能,则认为是错误.
XML不是有效的JSON(它真的很讨厌开放<).您可以更改dataType为'xml'(或不执行任何操作)或实际从服务器发出纯JSON.
| 归档时间: |
|
| 查看次数: |
18828 次 |
| 最近记录: |