【问题标题】:Unable to Return jSon string from WCF Service from jQuery无法从 jQuery 的 WCF 服务返回 json 字符串
【发布时间】:2012-03-21 17:25:32
【问题描述】:

我有一些返回 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。

【问题讨论】:

  • 尝试使用 {"bookName":"Oil","buisinessDay":"02/03/2012"} 而不是 "bookName=Oil&buisinessDay=02032012"....将数据传递给 ajax请求。
  • 您检查过返回的 JSON 确实有效吗?
  • 是的,我试过了,{"bookName":"Oil","buisinessDay":"02/03/2012"}但这不起作用。
  • Web 服务确实返回了一个有效的 JSON 字符串。

标签: jquery json wcf jsonp


【解决方案1】:

如果端口或主机不是同一个域服务必须返回 JSONP 或者你需要代理请求

【讨论】:

  • 它们都在同一个端口。
  • 在这种情况下,您不需要 jsonp 数据类型,只需要 json
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 2014-06-14
相关资源
最近更新 更多