Jquery ajax 在调用 rest Web 服务时失败

Jquery ajax fail when calling rest webservice

本文关键字:服务 失败 Web rest ajax 调用 Jquery      更新时间:2023-09-26

我有一组 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 始终为空。状态始终为错误。errorThrow也始终为空。

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

您的 JSON 不正确。尝试使用这个

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

从一个 url 导航到另一个 url 时,会弹出一个跨域错误。尝试这样做。使用 ajax 调用同一 url 上的函数,并从那里使用 CURL 请求到您的 Web 服务 url。