0

Even though I can get a JSON file that gets downloaded, when I go to my remote URL. When I use the following code in my javascript, nothing happens:

<input type="text" id="query" /><button>search</button><br />
<div id="results">

</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
//var url='http://search.twitter.com/search.json?callback=?&q=google+wave';
var url = url + '/Services/GetNthClosestDriverLocationToMe?callback=?&latitude=30&longitude=-97.6&nth=2';
var query;
    $('button').click(function(){
    query=$("#query").val();
    $.getJSON(url+query,function(json){
        $.each(json.results,function(i,tweet){
           //$("#results").append('<p><img src="'+tweet.profile_image_url+'" widt="48" height="48" />'+tweet.text+'</p>');
            $("#results").append('<p>'+tweet.Name+'</p>');
        });
    });
});
});
</script>

Notice the commented out twitter URL that works just fine. This makes me think that it has something to do with my URL, even though I am able to get the JSON file when i simply go to the url from the browser. Also, note that my URL is on a remote domain.

4
  • 2
    is there a reason u are using jquery 1.3.2?
    – Piotr Kula
    May 7, 2011 at 15:06
  • are getting any errors in the console ?
    – mcgrailm
    May 7, 2011 at 15:07
  • are you sure that you url var is ok. http : //www.domain.com and not just www.domain.com or domain.com . that can cause a problem.
    – Piotr Kula
    May 7, 2011 at 15:08
  • What is $("#query").val() ? Maybe you need some ampersand there(between url and query) . You'll also need to encode it. api.jquery.com/serialize may be helpful here.
    – Dr.Molle
    May 7, 2011 at 15:13

2 Answers 2

0

Your url var is undefined in your code. This might be the problem. If not, please put your full code. Your url might be malformed.

2
  • The url is good. I didn't want to reveal it, but the parameters are there in case there is a problem. Plus I got it to work by simply replacing the $.each block by the following line: $("#results").append('<p>'+json.Name+ '</p>'); My json file looks as follows: { "Id":6, "UserId":"rjones", "Name":"Rick Jones", "TruckType":"Van 20\u0027" }
    – L8Again
    May 7, 2011 at 20:23
  • I still don't know why the previously stated $.each block didn't work.
    – L8Again
    May 7, 2011 at 20:27
0

Thanks for all the replies. After firing up fire-bug, the problem is not in the code, but in my JSON (posted in one of my replies above). Unlike the Twitter result, mine is not a collection, and thus it was erroring out on $.each.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.