我想让我的ajax调用工作来连接到使用ServiceStack的API。我遇到的问题是身份验证。在C#我做的呼叫是这样的:ServiceStack + Ajax身份验证呼叫
string json = "";
JsonServiceClient client;
client = new JsonServiceClient("https://api.domain.com");
client.SetCredentials("test", "test");
client.AlwaysSendBasicAuthHeader = true;
try
{
List<AppleResponse> response = client.Get<List<AppleResponse>>("/v1/getdata?id=1");
json = response.ToJson();
}
catch (Exception ex)
{
json = ex.Message;
}
JArray v = JArray.Parse(json);
var total = v[0]["total"].ToString();
我得到的总的价值,例如总= 10 现在我想做的使用Ajax调用同样的事情。这里是未经验证即可调用示例:
function Getdata(id) {
$.ajax({
url: '//api.domain.com/v1/getdata',
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ id_s: id }),
success: function (f) {
$.each(f.d, function (i, e) {
alert(e.total);
});
},
cache: false
});
}
有谁知道我怎么能加入身份验证,但不显示在JavaScript标记中的用户名/密码?非常感谢。
可以保存在头的身份验证令牌,并将其发送的每当你做阿贾克斯呼叫。 – reptildarat
谢谢reptildarat,你知道确切的语法吗?像这样:$ .ajax({url:'//api.domain.com/v1/getdata', type:“POST”, contentType:“application/json; charset = utf-8”,auth :<%= client %>,数据:... – carol1287