javascript - 为什么 Jquery/ajax 调用 RESTful 服务无法获得成功回调?

标签 javascript jquery asp.net ajax rest

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 
}

关于javascript - 为什么 Jquery/ajax 调用 RESTful 服务无法获得成功回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35423863/

相关文章:

javascript - 除非首先在 DevTools 控制台中进行评估,否则 Backbone model.save() 会失败

javascript - sweet.js 无法返回 #`else`

jquery - 使用 jQuery 弹跳图像

c# - 在 asp.net 中克服发送电子邮件延迟的方法

javascript - AngularJS 两个日期之间的区别

javascript - For Loop 没有正确应用第 n 个 child

javascript - 附加到 window.resize 事件的循环中的分辨率顺序

JavaScript insertAfter 和 insertBefore

css - 背景图像未在 Chrome 或 IE 中显示

c# - 没有现有记录时填充数据时出错。 ASP.NET C#