如何从成功中获取ajax响应并使用jQuery将其分配给变量?

大家好我从ajax得到回复有问题。 如果我在控制台中显示它。 我可以看到它。 但是如何在变量中分配它?

这就是我所拥有的。 在我的PHP代码中,我有这个

public function checkPassword($password){ $username = $this->session->userdata('username'); $validate = $this->members_model->checkPassword($password,$username); echo $validate; } 

在我的jquery中我有这个

 $('#existing').on('keyup',function(){ var id = 'session->userdata("user_id"); ?>'; var password_url = ''; $.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!'); } } }); }); 

 $.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是成功函数的响应变量。

indexof返回指定值第一次出现的调用String对象内的索引,从fromIndex开始搜索, 如果找不到该值则返回-1

尝试这样的事情

   

在您的成功响应中,您将获得在php中输出的内容。 如果你想获得一个数组或数据集,你可以在你的PHP脚本中的json中编码它

 echo json_encode($validate); 

然后在你的jquery中你可以像这样使用这个响应

 var responseData = jQuery.parseJSON(response); console.log(responseData); 

console.log将在浏览器控制台中打印json对象。 你可以像这样使用这个json对象

 responseData.some_data 

Ajax是异步的,所以在ajax方法返回后你可以访问它:

 $('#existing').on('keyup',function(){ var id = 'session->userdata("user_id"); ?>'; var password_url = ''; $.ajax({ type: 'POST', url: password_url, data: '', dataType: 'json' }).then(function(response){ var k; if(response == 1){ k = response; //call another function that needs k here } }); }); 
  $.ajax({ type: 'POST', url: password_url, data: '', dataType: 'json', success: function(response){ k=response; } }); 
 var k = null; $('#existing').on('keyup', function() { var id = 'session->userdata("user_id"); ?>', password_url = ''; $.ajax({ type : 'POST', url : password_url, success : function(data) { if(data === '1') { k = data; } } }); }); 

响应参数本身包含数据,因此只需将其分配给变量并使用它。

  $.ajax({ type: 'POST', url: password_url, success: function(response){ if(parseInt(response) == 1){ var k = response; } } }); 

您的响应数据是成功函数的response变量。 由于响应类型是json,因此可以将其直接分配给javaScript变量。

你也比较错误尝试if(g == '1')而不是if(g == 1) 。 你得到一个字符串作为响应和你的检查相等与数字类型在任何点都不相等。

即: –

 $.ajax({ type: 'POST', url: password_url, data: '', dataType: 'json', contentType:"application/json",// Add Content type too success: function(response){ k=response; } }); 

如果您的json响应如下所示

 {"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }} 

你可以访问menuitem数组

 success: function(response){ k=response.menu.popup.menuitem; } 

文件名称votepost.php

 response=="true") { echo 'true'; } else { echo 'false'; } ?> 

ajax响应使用$ .trim作为IF ELSE

 $.post("votepost.php", {CSID:csid,VOTEID:voteid,MYID:myid,USERTYPE:usertype}, function (data) { if($.trim(data)=='true') { alert('ok'); } else { alert('error'); } }); 

我希望你能解决你的问题

您可以创建js空白数组并将其分配给同一个数组。

 var resp = []; jQuery.ajax({ dataType: "json", method: 'post', url: 'ajax.php', async: false, data: {postData: 'postData'}, success: function(data){ resp.push(data); } });