How to get the ajax response from success and assign it in a variable using jQuery?
ajax, javascript, jquery, php
Solution
$.ajax({
type: 'POST',
url: password_url,
data: '',
dataType: 'json',
success: function(response){
var k=response;
if(k.indexOf("1") != -1)
$('#existing_info').html('Password is VALID');
else
$('#existing_info').html('Password is INVALID!');
}
});
`response` is in response variable of success function.
indexof returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex, returns -1 if the value is not found.
Problem
Hello guys I have a problem in getting the response from my ajax. If I display it in the console. I can view it. But How do I assign it in a variable? Here's what I have. In my PHP code I have this ``` public function checkPassword($password){ $username = $this->session->userdata('username'); $validate = $this->members_model->checkPassword($password,$username); echo $validate; } ``` In my jquery I have this ``` $('#existing').on('keyup',function(){ var id = '<?php echo $this->session->userdata("user_id"); ?>'; var password_url = '<?php echo site_url("member/checkPassword/' +id+ '"); ?>'; $.ajax({ type: 'POST', url: password_url, data: '', dataType: 'json', success: function(response){ var g = response; if(g == 1){ $('#existing_info').html('Password is VALID'); //Doesn't display the VALID if the response is 1. Why? }else{ $('#existing_info').html('Password is INVALID!'); } } }); }); ```