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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.