2013-08-06 108 views
0

我想调用一个web服务方法GetUserLandingPage(用户名,密码)使用jQuery的Ajax。 这里是我的代码使用ajax调用WCF Web服务

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错误的请求故障运行页面occuring.Any想法什么是我的问题吗?

回答

0

您需要在呼叫中提供授权标头。只是将用户名和密码数据转换为json是不正确的。首先你需要base64编码你的字符串。并在阿贾克斯呼吁这一点。

(该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