简体   繁体   English

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

[英]Unable to Return jSon string from WCF Service from jQuery

I am having some WCF Services which returns a JSON String. 我有一些WCF服务,该服务返回JSON字符串。 The URL for it is, http://localhost:50377/DailyNote/AggregationService/PnlByBookName/ {DailyPnl}/Book?bookName={Oil}&buisinessDay={03/02/2011} 它的URL是http:// localhost:50377 / DailyNote / AggregationService / PnlByBookName / {DailyPnl} / Book?bookName = {Oil}&buisinessDay = {03/02/2011}

Now I want to get this string in HTML using jQuery. 现在,我想使用jQuery在HTML中获取此字符串。 The javascript is: 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();            
}); 

On execution, the alert form the Service Failed is Triggered with xhr.status as 200 & xhr.statusText as Success. 执行时,将触发服务失败的警报表单,其中xhr.status为200且xhr.statusText为Success。 I am able to call the Service but am unable to receive the jSon back. 我可以致电服务中心,但无法收到jSon。

如果端口或主机不是同一域服务,则必须返回JSONP,否则您将需要代理请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM