Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调用rest webservice时Jquery ajax失败_Javascript_Jquery_Ajax_Json_Rest - Fatal编程技术网

Javascript 调用rest webservice时Jquery ajax失败

Javascript 调用rest webservice时Jquery ajax失败,javascript,jquery,ajax,json,rest,Javascript,Jquery,Ajax,Json,Rest,我有一组REST URI,在服务器上进行身份验证后可以访问这些URI。此服务接收带有登录信息的JSON输入,并检索带有会话ID的JSON输出 当使用Rest客户端(如chrome扩展)时,一切正常 现在我想用JS实现它,但尽管返回失败,我看不到任何错误细节(错误消息为空),也找不到代码中缺少的内容 $.ajax({ // the URL for the request url: "https://host002:50000/b1s/v1/Login", // the d

我有一组REST URI,在服务器上进行身份验证后可以访问这些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!" );
    }
});
$.ajax({
//请求的URL
url:“https://host002:50000/b1s/v1/Login",
//要发送的数据(将转换为查询字符串)
数据:{
用户名:“经理”,
密码:“1234”,
公司数据库:“客户001”
},
//这是一个POST还是GET请求
类型:“POST”,
//我们期望返回的数据类型
数据类型:“json”,
//请求成功时要运行的代码;
//响应被传递给函数
成功:函数(json){
$(“”).text(json.title).appendTo(“body”);
$(“”).html(json.html).appendTo(“body”);
},
//请求失败时要运行的代码;原始请求和
//状态代码被传递给函数
错误:函数(xhr、状态、错误抛出){
警报(“抱歉,出现问题!”+xhr.responseText);
log(“错误:+errorshown”);
控制台日志(“状态:+状态”);
console.dir(xhr);
},
//无论成功或失败都要运行的代码
完成:功能(xhr,状态){
警报(“请求已完成!”);
}
});
xhr.responseText始终为空。状态总是错误的。ErrorSprown也始终为空


我也尝试了$post方法,但得到了相同的行为。

您的JSON不正确。试试这个

data: JSON.stringify({UserName: "manager", Password: "1234", CompanyDB: "CUS_001"});

当从一个url导航到另一个url时,会弹出一个跨域错误。 试着这样做。
使用ajax调用同一url上的函数,然后使用CURL请求web服务url。

这可能是一个跨源限制。剩下的服务是你的吗?如果是这样的话,您是否在其上启用了crossorigin requeusts?您的数据对象不是JSON。我考虑过它,但正如我所提到的,它在Chrome/Firefox上的REST客户端上工作。因此,我认为不应该有交叉原产地限制。是的,REST服务位于另一个域的远程服务器上。Tks!我相信它适用于扩展,因为权限是在清单中明确设置的。如果您使用的是Firebug之类的检查器,响应头在任何情况下都会给您一些线索。我认为服务器返回的JSON响应不正确,因为您的ajax请求使用JSON格式。谢谢您的输入。但这不是原因。换了之后,我得到了同样的问题。