0

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.

halfer
20.2k20 gold badges111 silver badges208 bronze badges
asked Oct 6, 2017 at 7:23
4
  • What should the response be then? i.e what's the expected result Commented Oct 6, 2017 at 7:25
  • Plase replace data : {email : email}, with data : {'email' : email}, Commented 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 page Commented Oct 6, 2017 at 7:29
  • put a die after echo $email; this will works like a charm. Commented Oct 6, 2017 at 11:15

3 Answers 3

4

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);
}
answered Oct 6, 2017 at 7:28
Sign up to request clarification or add additional context in comments.

3 Comments

Why use json though?? And not my upvote.. Also seems OP is just testing the response on Ajax call
because using json we can add more data to response easily in future @Akintunde
its working. thank you. but is it necessary to set dataType in ajax function.
0

Instead of

echo $email;

use:

$response = ["email" => $email];
return json_encode($response);

And parse JSON, on client side, using JSON.parse.

answered Oct 6, 2017 at 7:26

4 Comments

you can send plain text over ajax, why use json?
@madalinivascu Plain text can be used, but using JSON gives more idea and in future, we can add more data to response.
i think it is not necessary to set dataType as json, because there is only one value. but anyways, its working now
@geeth I never said that dataType as json is required. However, sending a JSON response is recommended.
0

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(); }

answered Aug 8, 2020 at 10:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.