[英]Jquery ajax fail when calling rest webservice
在服务器上进行身份验证后,我可以访问一组REST URI。 该服务接受带有登录信息的JSON输入,并获取带有会话ID的JSON输出。
当使用Rest客户端(例如chrome扩展程序)时,一切正常。
现在,我想使用JS来实现它,尽管返回失败,但是我看不到任何错误的详细信息(错误消息为空),也找不到我的代码中所缺少的内容。
$.ajax({
// the URL for the request
url: "https://host002:50000/b1s/v1/Login",
// the data to send (will be converted to a query string)
data: {
UserName: "manager",
Password: "1234",
CompanyDB: "CUS_001"
},
// whether this is a POST or GET request
type: "POST",
// the type of data we expect back
dataType : "json",
// code to run if the request succeeds;
// the response is passed to the function
success: function( json ) {
$( "<h1/>" ).text( json.title ).appendTo( "body" );
$( "<div class=\"content\"/>").html( json.html ).appendTo( "body" );
},
// code to run if the request fails; the raw request and
// status codes are passed to the function
error: function( xhr, status, errorThrown ) {
alert( "Sorry, there was a problem! " + xhr.responseText);
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
},
// code to run regardless of success or failure
complete: function( xhr, status ) {
alert( "The request is complete!" );
}
});
xhr.responseText始终为空。 状态始终为错误。 errorThrown始终也为空白。
我也尝试了$ post方法,但行为相同。
您的JSON不正确。 试试这个
data: JSON.stringify({UserName: "manager", Password: "1234", CompanyDB: "CUS_001"});
从一个网址导航到另一个网址时,会弹出一个跨域错误信息。 尝试这样做。 使用ajax在相同的URL上调用函数,然后从那里使用CURL请求到您的Web服务URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.