【发布时间】:2015-02-23 19:08:13
【问题描述】:
我正在尝试使用 WCF Web 服务。该服务在浏览器中以 Json 格式返回结果,但是当我尝试通过 ajax 调用获取数据时,它给出了错误。我的代码如下。该服务在公共服务器上运行,并在 angularjs 中使用。在 angularjs 中,服务返回 Json 数据。这是我第一次使用 WCF svc 服务调用。
var Type;
var Url;
var Data;
var ContentType;
var DataType;
var ProcessData;
function WCFJSON() {
Type = "GET";
Url = "http://server.com/main/webservices/econnect.svc/getdata";
ContentType = "application/json; charset=utf-8";
DataType = "json"; varProcessData = true;
CallService();
}
function CallService() {
$.ajax({
type: Type, //GET or POST or PUT or DELETE verb
url: Url, // Location of the service
contentType: ContentType, // content type sent to server
dataType: DataType, //Expected data format from server
processdata: ProcessData, //True or False
success: function(msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}
function ServiceFailed(result) {
alert('Service call failed: ' + result.status + '' + result.statusText);
Type = null;
varUrl = null;
Data = null;
ContentType = null;
DataType = null;
ProcessData = null;
}
function ServiceSucceeded(result) {
if (DataType == "json") {
resultObject = result.GetUserResult;
for (i = 0; i < resultObject.length; i++) {
alert(resultObject[i]);
}
}
}
function ServiceFailed(xhr) {
alert("Error:" + xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
$(document).ready(
function() {
WCFJSON();
}
【问题讨论】:
标签: javascript jquery ajax web-services wcf