WCF服务没有响应通过AJAX调用

时间:2011-05-13 14:32:11

标签: jquery ajax wcf response

我知道这个问题已被多次询问,但我无法找到解决我特定问题的方法。

我有一个使用webHttpBinding的WCF Web服务,我试图通过jquery $ .ajax()调用从网页调用。

serviceFailed = function (a, b, c) {
    alert('Failure: ' + c);
}

$.ajax({
    type: "GET",
    url: "http://localhost/KasraNet.Services.Centurian.WebService/CenturianAdmin.svc/bob",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    processdata: true,
    error: serviceFailed,
    success: function (msg) {
        alert('Success: ' + msg);
    }
});

运行时,会触发成功警报,但msg为空。

使用FireBug,我可以看到响应完全为空,在使用HttpFox时,返回错误NS_ERROR_DOM_BAD_URI。我不知道这是一个跨浏览器的问题,因为我要去localhost并且该服务也在同一台机器上运行(我甚至试图将 localhost 更改为机器名而没有成功)。

因此定义了服务。

<OperationContract(), DataContractFormat()>
<WebInvoke(BodyStyle:=WebMessageBodyStyle.Wrapped, Method:="GET", ResponseFormat:=WebMessageFormat.Json)> _
Function bob() As String

ajax调用的Request和Response标头是

Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.17) Gecko/20110420 Firefox/3.6.17 (.NET CLR 3.5.30729)
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: application/json; charset=utf-8
Origin: null

Server: Microsoft-IIS/5.1
Date: Fri, 13 May 2011 14:20:53 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
Content-Length: 19
Cache-Control: private
Content-Type: application/json; charset=utf-8

任何帮助都会非常感激 - 我真的开始拔头发了!

编辑: 现在有趣的是,如果我将网址粘贴到浏览器地址栏中,则会以json格式的服务方法结果启动下载。

由于 约翰

2 个答案:

答案 0 :(得分:1)

好吧,我有点麻木了。

我考虑过跨域问题,并意识到我正在打开一个指向该文件的直接链接,而不是通过IIS。

通过这个创建VD并打开页面,我期待的结果又回来了。

谢谢你花时间看这个问题,抱歉浪费你的时间。

答案 1 :(得分:0)

您的请求中的数据很可能是问题所在:

  • 您的WCF功能不期望任何输入数据。

  • 由于你传递一个字符串作为数据(它看起来像一个空的JSON对象,但它实际上是一个字符串),jQuery会将它附加到这样的URL:http://localhost/KasraNet.Services.Centurian.WebService/CenturianAdmin.svc/bob?{}但这是一个无效的URL并且IIS将拒绝它。

相关问题