gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:40:49 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com