我正在使用一些返回JSON字符串的WCF服务。 它的网址是http://localhost:50377/DailyNote/AggregationService/PnlByBookName/ {DailyPnl} / Book?bookName = {Oil}& buisinessDay = {03/02/2011}
现在我想使用jQuery在HTML中获取此字符串。 javascript是:
var Type; var Url; var Data; var ContentType; var DataType; var ProcessData; var method;
function CallService() {
$.ajax({
type: Type,
url: Url,
data: "bookName=Oil&buisinessDay=02032012",
contentType: ContentType,
dataType: DataType,
processdata: ProcessData,
success: function (msg) {
alert("service success");
ServiceSucceeded(msg);
},
error: ServiceFailed
});
}
function ServiceFailed(xhr) {
alert("service fail: " + xhr.status + " : " + xhr.statusText);
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;if (err) error(err); else error({ Message: "Unknown server error." });
} return;
}
function ServiceSucceeded(result) {
alert("Service Success");
if (DataType == "jsonp") {
resultObject = result.GetPnlByBookHierarchy;
alert(resultObject);
}
}
function GetData() {
Type = "GET";
Url = "http://localhost:50377/DailyNote/AggregationService/PnlByBookName/DailyPnl/Book?";
DataType = "jsonp";
ProcessData = true;
method = "PnlByBookName";
ContentType = "application/javascript";
CallService();
}
$(document).ready(function () {
GetData();
});
执行时,将触发服务失败的警报,xhr.status为200& xhr.statusText成功。 我可以致电服务但无法收到jSon。
答案 0 :(得分:0)
如果端口或主机不相同,域服务必须返回JSONP,或者您需要代理请求