为什么无法在 Jquery/ajax 调用中获取对 RESTful 服务的成功回调

Why Unable to get success callback in Jquery/ajax call to RESTful services?

本文关键字:RESTful 获取 服务 回调 成功 调用 Jquery ajax 为什么      更新时间:2023-09-26
function GET() {
        jQuery.support.cors = true;
        $.ajax({
            url: 'http://localhost:32253/api/UserDetail/GetRoleDetails',
            type: 'GET',
            async:true,
            contentType: "application/json",
            dataType: 'json',
            xhrFields: {
                withCredentials: true
            },
            error: function (xhr, status)
            {
                alert(status);
            },
            success: function (data) {
                alert("Success!");
            }
        });
        return false;
    }

我对 ajax 非常陌生,我尝试对我的服务方法进行 ajax 调用,该方法返回值。但是成功回调函数永远不会被触发。我只得到错误。可能是什么原因?我尝试使用 dataType 来 jsonp 和其他类似的解决方案,这些解决方案在 Stackoverflow 中发现了这个问题。什么都没有解决。我得到的服务器响应为 200 oK。

试试这段代码

$.ajax({
        type: "POST",
        url: URL,
        async: true,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        jsonp: "onJSONPLoad",
        jsonpCallback: "newarticlescallback",
        crossDomain: "false",
        beforeSend: function (xhr) {
        },
        error: function (request, status, error) {
            console.log("Error In Web Service...." + request.responseText);
            return false;
        },
        success: ajax_success,
        complete: function (xhr, status) {
        }
    });

在页面中创建函数"ajax_success"像下面一样

function ajax_success(parsedJSON) { //you code here 
}

这可能会对你有所帮助试试这个代码

 $.ajax({
        type: "POST",
        url: URL,
        async: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        jsonp: "onJSONPLoad",
        jsonpCallback: "newarticlescallback",
        crossDomain: "false",
        beforeSend: function (xhr) {
        },
        error: function (request, status, error) {
            console.log("Error In Web Service...." + request.responseText);
            return false;
        },
        success: function (data) {
            Result = data;
        },
        complete: function (xhr, status) {
        }
    });