来自$ .ajax的wcf调用在chrome中不起作用

时间:2012-10-29 09:48:02

标签: wcf jquery

尝试从jquery中使用简单的WCF服务时遇到了一些特殊问题。请参阅以下代码:

$(document).ready(function () { // Initialize the carousel on the header part of the page $("#myCarousel").carousel({ interval: 2000 });

// The handler for the submit button (login form)
$("#submit").click(function () {
    var sendData = '{"username": "' + 
                     $("#usernameTB").val() + 
                   '", "password": "' + 
                     CryptoJS.MD5($("#passwordTB").val()) + 
                   '"}';

    $.ajax("Services/LoginUser.svc/Login", {
        cache: false,
        type: "POST",
        data: sendData,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processData: true,
        complete: ServiceCompleted
    });
});

// The handler for the submit button (login form) $("#submit").click(function () { var sendData = '{"username": "' + $("#usernameTB").val() + '", "password": "' + CryptoJS.MD5($("#passwordTB").val()) + '"}'; $.ajax("Services/LoginUser.svc/Login", { cache: false, type: "POST", data: sendData, contentType: "application/json; charset=utf-8", dataType: "json", processData: true, complete: ServiceCompleted }); });

当我在IE中使用此代码时,它就像一个魅力,即执行调用并返回我期望的内容,请参阅下面的请求/响应的fiddler转储:

POST http://localhost/CanDoIT/Services/LoginUser.svc/Login HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost/CanDoIT/default.htm
Accept-Language: nl-NL,en-US;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: localhost
Content-Length: 64
Connection: Keep-Alive
Pragma: no-cache

{"username": "", "password": "d41d8cd98f00b204e9800998ecf8427e"}

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 10
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 29 Oct 2012 09:31:15 GMT

{"d":null}

如果我使用Chrome执行相同操作,则会收到以下请求/响应转储:

POST http://localhost/CanDoIT/Services/LoginUser.svc/Login HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 64
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
Content-Type: application/json; charset=UTF-8
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost/CanDoIT/default.htm?
Accept-Encoding: gzip,deflate,sdch
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

{"username": "", "password": "d41d8cd98f00b204e9800998ecf8427e"}

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 10
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 29 Oct 2012 09:30:27 GMT

{"d":null}

现在,警报没有显示{{d“:null}文本。

有谁知道阻止我使用responseText的区别是什么?

1 个答案:

答案 0 :(得分:0)

我在谷歌搜索后找到解决问题的方法......

似乎我的表单上的提交按钮的单击处理程序必须返回false才能使其正常工作。

我不知道为什么,但它有效...

相关问题