0

我正在尝试使用 jquery ajax 调用 Web 服务方法 GetUserLandingPage(username,password)。这是我的代码

    var userName=$("#txt_userName").val().trim();
    var password=$("#txt_userPassword").val().trim();

    var data={
    "username":userName,
    "password":password
    };
    var jsonData=JSON.stringify(data);
    $.ajax({
    url: "http://www.coolcomma.com/user.svc/GetUserLandingPage",
    type:"GET",
    data:jsonData,
    contentType: "application/json",
    dataType: "jsonp",
    crossDomain : true,
    success: function(result){
        alert(result);
    }
});

当我在浏览器中运行页面时出现 400 错误请求错误。知道我的问题是什么吗?

4

1 回答 1

0

您需要在调用中提供 Authorization 标头。仅将用户名和密码数据转换为 json 是不正确的。您首先需要对字符串进行 base64 编码。在 ajax 调用中。

(base64Encodedstring 应该是“用户名:密码”)

$.ajax({
    headers : {
     "Authorization" : "Basic base64Encodedstring"
    },
    type: "GET",
    xhrFields: {
        withCredentials : true
    },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success : function(data) {
        console.log("ok!");
    },
    error : function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus + ' | ' + errorThrown);
    }
});

另外,看看这个网址:http ://sameproblemmorecode.blogspot.se/2011/10/creating-secure-restfull-wcf-service.html

于 2013-08-06T10:30:18.053 回答