Jump to content

Save a json object into a variable


netpumber

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.