I have an AJAX call in my codeigniter project. Here is my code:
in 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();
}
});
}
});
and controller :
public function forgotPassword() {
$email = $this->input->post('email');
echo $email;
}
but the response contains only the html content from my view. I couldn't identify what is happening.
-
What should the response be then? i.e what's the expected resultRotimi– Rotimi2017年10月06日 07:25:23 +00:00Commented Oct 6, 2017 at 7:25
-
Plase replace data : {email : email}, with data : {'email' : email},Ferhat BAŞ– Ferhat BAŞ2017年10月06日 07:26:52 +00:00Commented Oct 6, 2017 at 7:26
-
@ankitunde i just return the email id posted fron ajax request . but it displays whole html code in my view pagegeeth– geeth2017年10月06日 07:29:29 +00:00Commented Oct 6, 2017 at 7:29
-
put a die after echo $email; this will works like a charm.gaurav malik– gaurav malik2017年10月06日 11:15:25 +00:00Commented Oct 6, 2017 at 11:15
3 Answers 3
change your jquery code to
$('#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();
}
});
}
});
change your controller code like
public function forgotPassword() {
$email = $this->input->post('email');
$response = ["email" => $email];
echo json_encode($response);
}
3 Comments
Instead of
echo $email;
use:
$response = ["email" => $email];
return json_encode($response);
And parse JSON, on client side, using JSON.parse.
4 Comments
JSON gives more idea and in future, we can add more data to response.hi maybe i can help someone, i had the same problem, in my case the error was here "url : base_url + 'Home/forgotPassword'"
in this example i have to pass all way like this url : /anotherdirectory/Home/forgotPassword.php', take a look in your "url"
$.ajax({
url : "change here fo works"',
type : 'POST',
data : {email : email},
dataType:'json',
success: function(data) {
console.log(data);
//location.reload();
}
Comments
Explore related questions
See similar questions with these tags.