gpt4 book ai didi

php - 使用 JQuery 从 post() 接收响应

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

我有一个通过纯 JavaScript 使用 Ajax(POST 方法)的脚本。现在我正在尝试学习 JQuery,因为它看起来更容易,但我在请求响应方面遇到问题

$.post('ajax/wrapper.php', {'desc':$('#description').val()},
function(data){
$('#something').html(data);
});

当我这样做时,请求已成功发送。我可以看到数据库上的更改,但响应没有显示在 div#something 中。我也尝试过

...
$('#something').html($(data));
...

提前致谢

最佳答案

commenting out the following line worked: header("Content-type: text/xml; charset=iso-8859-1")

参见this document以及 jQuery.ajax 中的 dataType 参数。您应该返回带有 text/html MIME 类型的 HTML,或者将 dataType 参数设置为“html”:

$.ajax('ajax/wrapper.php', {
data: {'desc':$('#description').val()},
dataType: 'html',
type: 'POST',
success: function(data){
$('#something').html(data);
}
}).then(console.log); // debug

第一个更好,因为您确实返回 HTML,而 text/html 是正确的 MIME。

关于php - 使用 JQuery 从 post() 接收响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6785166/

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