如何使用 jquery ajax 调用传递网络服务的凭据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14579478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to pass credentials for a webservice using jquery ajax call?
提问by Sriks
I am using the jquery ajax call like this:
我正在使用这样的 jquery ajax 调用:
$.ajax({
url: WEBSERVICE_URL,
type: "GET",
dataType: "application/json; charset=utf-8",
username: "admin", // Most SAP web services require credentials
password: "admin",
processData: false,
contentType: "application/json",
success: function() {
alert("success");
},
error: function() {
alert("ERROR");
},
});
still the call is not going to web service. Everytime I am getting ERROR alert. Can some body help me on this please?
电话仍然不会转到网络服务。每次我收到错误警报。请有人帮我解决这个问题吗?
采纳答案by Mortalus
Try using post for the method type, most webservices are secured and require transimssion using post and not Get
尝试使用 post 作为方法类型,大多数网络服务都是安全的,需要使用 post 而不是 Get 进行传输
plus to help you debug the error and a response text to your error.
加上帮助您调试错误和对错误的响应文本。
$.ajax({
url: WEBSERVICE_URL,
type: "POST", //This is what you should chage
dataType: "application/json; charset=utf-8",
username: "admin", // Most SAP web services require credentials
password: "admin",
processData: false,
contentType: "application/json",
success: function () {
alert("success");
},
error: function (xhr, ajaxOptions, thrownError) { //Add these parameters to display the required response
alert(xhr.status);
alert(xhr.responseText);
},
});
回答by jiantongc
If you are doing cross domain request:
如果您正在执行跨域请求:
$.ajax({
url: "yoururl",
type: "GET",
dataType: 'json',
xhrFields: {
withCredentials: true
}
});