Ajax Post Is Not Returning Data

I used API in my project to get data from other server in online.

In my project i used Jquery to post some values to the PHP function. In that PHP function i try to call the API and retrieve some data from API and saved in my DB and the DB will return some ID. The ID is the return data to the Jquery. But the loop continue after all these functions over, the functions is running again without returning the data.

The main thing is it runs perfectly all time. The error is coming on only sometimes, ie may be ones in 20 times running.

Here i shows the sample of the coding. Please support me to solve the issues.

In Jquery

code:

$.post(url, //goes to the PHP function actionGetResult()

    {


        cacheid:cid,


		rtid:rtid,


		type:type,			


        user_id:userid,


        name:name


    },


    function (data)


    {


		alert(data);


			window.location.href=general_details_global.url.CPage+"?data;                    


    });

In PHP

public function actionGetResult() {

 if (isset($_GET['rtid'])) {


 	$api_obj = new API_Object();  [b]// API Class[/b]


$this->layout = '/layouts/details';


    $cacheid = $_GET['cacheid'];


	$rtid = $_GET['rtid'];


	$type = $_GET['type'];


	$user_id = $_GET['user_id'];   


	$name = $_GET['name'];


	[b]/*  I convert these to the array and to XML to give the data to API */[/b]


	$result = $api_obj->getResult($Input);  [b]// Calls to the API funtion and return data from API[/b]


	$this->saveDetails($result, $cacheid, $rtid);


 }

}

public function saveDetails($result, $cacheid, $rtid) {

$noid = Details::saveDetails($result);


echo $noid;

}

every time it runs successfully, But sometimes there is an error after all the things are running ie., calling the API and getting the result from API and saves in the DB also, but it not returning the data to the ajax.

In my log the error is saved as “undefined variable cacheid on line __”, ie the loop is running again and there is no value in the cacheid in the $_GET[‘cacheid’].

Please suggest me how to fix this issue.

Thanks in advance.,

Can one help me?..

This code stops my project with a great issue. Can any one please tell me why this happens…

Is there any issue in coding…