无法从jQuery返回WCF服务中的jSon字符串

时间:2012-03-03 13:00:38

标签: jquery json wcf jsonp

我正在使用一些返回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。

1 个答案:

答案 0 :(得分:0)

如果端口或主机不相同,域服务必须返回JSONP,或者您需要代理请求