简体   繁体   中英

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

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;
    }

I am very much new to ajax,I tried a ajax call to my services method which returns value.But the success callback function is never fired .I only get error.What might be the reason? I tried using dataType to jsonp and other similar solutions which had been found in Stackoverflow regarding this issue.Nothing worked out.I am getting server response as 200 oK.

try this code

$.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) {
        }
    });

create function "ajax_success" in ur page like below

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

this may be help you try this code

 $.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) {
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM