[英]Unable to call WCF Restful service from jQuery
以下是我来自jQuery脚本的代码:
$(document).ready(function () {
$.ajax({
cache: false,
type: "GET",
async: true,
dataType: "json",
url: "http://localhost:9002/SampleServices/Service/REST/Employees",
processdata: false,
error: ServiceFailed,
success: function () { alert("succeeded"); }
});
});
function ServiceFailed(result) {
alert('Service call failed: ' + result.status + '' + result.statusText);
Type = null;
Url = null;
Data = null;
ContentType = null;
DataType = null;
ProcessData = null;
}
function ServiceFailed(xhr) {
alert("error");
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
当我运行上面的代码时,它总是输入ServiceFailed(xhr)并警告“错误”。
当我尝试通过浏览器执行上述url(“ http:// localhost:9002 / SampleServices / Service / REST / Employees”)时,我得到以下JSON响应:{“ page”:1,“ records”:1,“ rows“:[{” cell“:[” 1“,” Haris“,” 21-03-1979“,” HR“],” id“:1}],” total“:1}
当我运行jQuery脚本时,Fiddler表示以下内容:
# Result Protocol Host URL Body Caching Content-Type Process Comments Custom
1 200 HTTP localhost:9002 /SampleServices/Service/REST/Employees?_=1340833203960 89 application/json; charset=utf-8 chrome:1448
carlosfigueira钉了它。
这是一个跨域请求,因此我需要为WCF服务在App.config文件中添加跨域支持
从文档中 ,错误回调函数接受三个参数: jqXHR
, textStatus
和errorThrown
。
您的方法只有一个参数。 您应该首先让错误方法采用三个参数并检查它们具有哪些值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.