3

I am using laravel to query google's search api. Here is the code that does that:

Route::get('google/(:any)', function($query)
{
    $uri = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".$query;
    $response = Httpful::get($uri)->send();
    $r = json_decode($response); 
    return Response::json($r);
});

This can be accessed by http://example.com/w/google/queryhere

I am also trying to make more than one requests in javascript and parsing the json.

$.when( $.ajax(google), $.ajax(bing), $.ajax(yahoo)).then(function(resp1, resp2, resp3)
{ 
    var obj = jQuery.parseJSON(resp1);
});

However, I am getting an unexpected syntax token when using the parseJSON method. I don't know where I went wrong.

1
  • 4
    can you provide the json response you are getting Commented Mar 10, 2013 at 15:31

1 Answer 1

2

First, $.ajax parses JSON responses on itself as it detects the content type - you don't need to do that manually.

Second, $.when applied on promises that resolve with multiple arguments is a bit obscure. Usually, a callback on an ajax deferred has 3 arguments: data, textStatus, jqXHR. Yet, the combined promise will resolve with them as an array (one array per deferred).

So change it to

$.when( $.ajax(google), $.ajax(bing), $.ajax(yahoo)).then(function(resp1, resp2, resp3){ 
    var obj = resp1[0];
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.