netpumber Posted July 8, 2014 Share Posted July 8, 2014 Hi! I have this code above: // Submit Data to ncbi. // Sends form's data to classController.php function NCBI_submit_data() { $formData = $('#blastx_form').serialize(); $php_method = 'ncbi_request'; $finalData = $formData + "&php_method=" + $php_method; $aR = ajaxReq('POST','../../classes/classController.php',$finalData,'json'); console.log($aR); } // General Ajax function function ajaxReq($method,$url,$data,$dataType) { $.ajax({ type: $method, url: $url, async: 'false', data: $data, dataType: $dataType, success: function(json, textStatus, jqXHR) { return json; }, error: function(jqXHR, textStatus, errorThrown) { console.log('Ajax call error: '+jqXHR.status+' '+errorThrown) } }); } Server returns a json object and i want to save it in aR variable. But console.log($aR); returns undefined Any idea on how to fix it ? Link to comment https://forums.phpfreaks.com/topic/289566-save-a-json-object-into-a-variable/ Share on other sites More sharing options...
fastsol Posted July 8, 2014 Share Posted July 8, 2014 Wouldn't this line return json; Just turn into this var aR = json; Link to comment https://forums.phpfreaks.com/topic/289566-save-a-json-object-into-a-variable/#findComment-1484260 Share on other sites More sharing options...
netpumber Posted July 8, 2014 Author Share Posted July 8, 2014 Hmm i created a global var $aR = ''; then in the // Submit Data to ncbi. // Sends form's data to classController.php function NCBI_submit_data() { $formData = $('#blastx_form').serialize(); $php_method = 'ncbi_request'; $finalData = $formData + "&php_method=" + $php_method; $aR = ajaxReq('POST','../../classes/classController.php',$finalData,'json'); console.log($aR); } // General Ajax function function ajaxReq($method,$url,$data,$dataType) { $.ajax({ type: $method, url: $url, async: 'false', data: $data, dataType: $dataType, success: function(json, textStatus, jqXHR) { $aR = json; }, error: function(jqXHR, textStatus, errorThrown) { console.log('Ajax call error: '+jqXHR.status+' '+errorThrown) } }); } But doesn't seem to work. Link to comment https://forums.phpfreaks.com/topic/289566-save-a-json-object-into-a-variable/#findComment-1484281 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.