gpt4 book ai didi

javascript - AJAX成功后的回调函数

转载 作者:行者123 更新时间:2023-11-28 00:10:15 25 4
gpt4 key购买 nike

我使用回调是因为我希望将我的数据填充到另一个域(通过使用我的 JavaScript)。

/*
*
* Description: This function is AJAX loader for Footer and processing Callback function as a response"
*
* @Param :actionName : The URL to be called
*
* */

function ajaxFooterLoader(actionName) {
$.ajax({
type: 'GET',
url: "http://localhost:8080/ajax/ttsGetContent.do?languageCode=en&productType=package&pageId=packageSearchResults&format=jsonp&includes=FOOTER",
dataType: "json",
success: function(response) {
//WHAT SHOULD I DO???
},
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
}

下划线是我通过 AJAX 进行 url 调用后得到的响应,我在 url 中传递格式,它直接返回我要调用的函数,即 processFooter。

但是,我不明白成功后我应该做什么,所以它直接调用我的回调调用返回的函数

来自服务器的 URL 响应

processFooter({
"copyRight": {
"description": null,
"id": null,
"name": "© 2015 AC",
"style": null
}
})

我的功能:这是 AJAX 返回的函数,并在 AJAX 调用上面定义

/*
*
* Description: This function will process footer
*
* @Param :dataFooter : it takes JSON as input
*
*
* */
function processFooter(dataFooter) {
mergeTemplateFooter(dataFooter);
}

请告诉我我的ajax成功应该如何调用这个函数?

提前致谢

最佳答案

由于您的服务器返回 JSONP,因此您必须告诉 jQuery 您需要 JSONP:

dataType: 'jsonp'

由于回调名称似乎是硬编码的,因此您还必须告诉 jQuery。否则它将生成一个随机函数名称。

jsonpCallback: 'processFooter'

jQuery 会自动创建一个具有这样名称的函数,因此您应该将 success 更改为

 success: function(response) {
mergeTemplateFooter(response);
},

您可以在$.ajax documentation中找到更多信息。 .

<小时/>

还要注意,您的 error 函数在这里将毫无用处,因为如果您使用 JSONP,jQuery 无法调用它:

error

Note: This handler is not called for cross-domain script and cross-domain JSONP requests.

关于javascript - AJAX成功后的回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30956870/

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