javascript - Parsing JSON data with a local AJAX request -
i've got php file on webserver executes queries mysql database.
i'm testing site on pc (locally) uses js file ajax request json data php file.
is possible this, or js file must put on same domain server of php file?
because console.log
of parsed data gives me error:
uncaught syntaxerror: unexpected token i
this ajax call
$.ajax({ method:"post", crossdomain:true, url:"url php file", data:{ query: "select * course_categories;" }, success: function(response){ var course_categories=json.parse(response); console.log(course_categories); var el=""; console.log(course_categories.length); for(var i=0;i<(course_categories.length);i++) { } }, error: function(request,error){ console.log("error: request " + request + "\nspecific error: " + error); }
while php call
<?php //get courses database , reply using json structure //$mysqli=new msqli("localhost","username","password","dbname"); $mysqli=new mysqli("localhost","hey","","db_name"); if(mysqli_connect_errno()) //returns number of error if there any, if not { echo json_encode("error connect dbms".mysqli_connect_error()); exit(); //closes connection } else { $query=$_post["query"]; //$query="select * course_categories"; $result=$mysqli->query($query); //do query (->query) setted $query, using $mysqli variable, , store data in $result if($result->num_rows >0) //if there @ least 1 row... { $myarray= array(); //...create array... while($row = $result->fetch_array(mysql_assoc)) { //...and fetch it. everytime operation returns row, $myarray[]=$row; //...and added myarray ([] means autoincrement). } } echo json_encode(utf8ize($myarray)); //free result $result->close(); //close connection $mysqli->close(); }
i did it. had do, remove crossdomain:true
line json parse data.
Comments
Post a Comment