Cross-Origin Request solution

Hello World ,
I am here to present a solution . The topic discusses about Cross-Origin Request . After having googled on the topic for hours I came to a conclusion that there is no perfect solution for this one . After that I tried to do it on my own .And I was succesful to come up with a solution . I will share it with you folks .
The prerequisites are
1. Basics of PHP .
2. Basics of Jquery (AJAX function ).
3. Running WAMP/LAMP server .
4. Google API key .Can be genereated by your google account . if you do not have one .
5. Basic understanding of Google Places API .

Now we start the action
The files are self explanatory .make a file call it anything . In my case jq2.php
//---------------------------------------------------------------------------------------


//includes the jquery file .
//the jquery scripting starts
$(document).ready(function(){ //document is made ready so that the program starts when we load this page
$("#subcat_search").keyup(function(){ //it tells that any key activity in the "subcat_search" filed will execute the query .
var link1="http://127.0.0.1/jqm/gs.php?needle="+$("#subcat_search").val();//we assemble the get link for the direction to our engine "gs.php".
$.ajax({url:link1,success:function(result){//ajax function is called sending the input string to "gs.php" .
$("#search_val").html(result); //result is stuffed in the label .
}});
})

});



//the input field for search string


//the output field for stuffing the output .


//---------------------------------------------------------------------------------------

Now We will include an engine . make a file . call it anything you like . in my case it is gs.php
//---------------------------------------------------------------------------------------
<?php
$head="https://maps.googleapis.com/maps/api/place/textsearch/json?query=";//our head
$key="your key here";//your key
$hay=$_GET['needle'];

$hay=str_replace(" ","+",$hay);//replacing the " " with "+" to design it as per the google's requirement
$kill=$head.$hay."&key=".$key;//assembling the string in proper way .
print file_get_contents($kill);
?>
//---------------------------------------------------------------------------------------

I have tried to keep the example as simple as possible . And because it executes the link on every keypress the quota of your api will be consumed pretty fast .


Of course there is not end to things we can do like putting the data into a table, sending to database and so on.

I will wait for feedback/queries and will try to answer them best to my abilities .
My mail id is harsh410@hotmail.com .
Constructive feedback will help me in doing more .

With best Regards
Harsh Mehta
Live and Let Live
Sign In or Register to comment.