gpt4 book ai didi

javascript - AJAX响应返回html内容

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

我的 codeigniter 项目中有一个 AJAX 调用。这是我的代码:

View 中:

$('#forgotPassword').click(function() {
var base_url = '<?php echo base_url()?>';
$('#forgotPasswordEmailError').text('');
var email = $('#forgotPasswordEmail').val();
console.log(email);
if(email == ''){
$('#forgotPasswordEmailError').text('Email is required');
}else{
$.ajax({
url : base_url + 'Home/forgotPassword',
type : 'POST',
data : {email : email},
success: function(data) {
console.log(data);
//location.reload();
}
});
}

});

和 Controller :

public function forgotPassword() { 
$email = $this->input->post('email');
echo $email;
}

但响应仅包含我看来的 html 内容。我无法确定发生了什么。

最佳答案

将您的 jquery 代码更改为

$('#forgotPassword').click(function() {
var base_url = '<?php echo base_url()?>';
$('#forgotPasswordEmailError').text('');
var email = $('#forgotPasswordEmail').val();
console.log(email);
if(email == ''){
$('#forgotPasswordEmailError').text('Email is required');
}else{
$.ajax({
url : base_url + 'Home/forgotPassword',
type : 'POST',
data : {email : email},
dataType:'json',
success: function(data) {
console.log(data);
//location.reload();
}
});
}

});

更改您的 Controller 代码,例如

public function forgotPassword() { 
$email = $this->input->post('email');
$response = ["email" => $email];
echo json_encode($response);
}

关于javascript - AJAX响应返回html内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46600395/

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