php - How to make a synchronous GET request in JavaScript? -




here code:

js code:

function savecategories(i){  	var categorieslist=json.parse(localstorage.getitem("categorieslist"));  	var code=localstorage.getitem("gamecode");  	  	if(i == categorieslist.length)  		return;  	  	var categoryname=categorieslist[i]['name'];  	var items=categorieslist[i]['items'];  	  	$.get('http://localhost:8080/yourfolder/newcategory.php',{gamecode:code,items:items,categoryname:categoryname},function(resp)  		{  			savecategories(i+1);  		});  }

php code:

<?php   header("access-control-allow-origin");    $host="127.0.0.1";  $user="root";  $pass="password";  $databasename="games";    $con=mysql_connect($host,$user,$pass,$databasename);    if(!$con)  {  	die("connection failed: ".mysqli_connect_error());  }    $code=$_get["gamecode"];  $name=$_get["categoryname"];  $items=$_get["items"];    $data=array();  foreach($items $item)  	$data[]=addcslashes($item);  $data=implode(",",$data);    $sql="insert games.categories(code,name,items) values ('$code','$name','$data')";  $result=mysql_query($sql,$con);    echo $result;  ?>  	
php file(newcategory.php) should code, category name , category items , insert them mysql db.

the savecategories recursive function should newcategory.php file foreach category(in index i), reason, proceed next iteration without getting result first get request before.

the unwanted result first category in index 0 not saved in mysql db.

i appreciate help. in advance.

use async: false in data

example:

 $.ajax({     url : "/your url",     type : "get",     async: false,     success : function(userstatus) {        //your logic     },     error: function() {      }  }); 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -