views:

102

answers:

4

Hi

Please find my code below. My problem is that I the inner jQuery.get() doesn't get executed. Could some one help me out with this?

jQuery(document).ready(function() { 
   $('.error').hide();  
   $(".button").click(function() {  
    // validate and process form here
      $('.error').hide();  
      var zipCode = $("input#name").val();  
      if (zipCode == "") {  
        $("label#name_error").show();  
        $("input#name").focus();  
        return false;  
      }
    var key = 'ac9c073a8e025308101307';
    var noOfDays = 5;
    var url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=' + zipCode + '&format=json&num_of_days=' + noOfDays + '&key=' + key;
    alert(url); 
    jQuery.get(url, function(test) { 
    alert(url); 
        $.each(test.data.weather, function(i, item){
        $("body").append("<p>Date: "+item.date+"</p>");
        if ( i == 3 ) return false;
  }); }, 'jsonp')();
});  
}); 

My html form looks like

<div id="contact_form">  
 <form name="contact" action="">  
   <fieldset>  
     <label for="name" id="name_label">Name</label>  
     <input type="text" name="name" id="name" size="30" value="" class="text-input" />  
     <label class="error" for="name" id="name_error">This field is required.</label>  

     <br />  
     <input type="submit" name="submit" class="button" id="submit_btn" value="Send" />  
   </fieldset>  
 </form>  
</div>  

I'd really appreciate any pointers.

Thanks!

+1  A: 

Part of the problem could be the extra set of parentheses after your .get() function call. You have:

jQuery.get(url, function(test) { 
    alert(url); 
    $.each(test.data.weather, function(i, item){
    $("body").append("<p>Date: "+item.date+"</p>");
    if ( i == 3 ) return false;
}); }, 'jsonp')();

It should be:

jQuery.get(url, function(test) { 
    alert(url); 
    $.each(test.data.weather, function(i, item){
        $("body").append("<p>Date: "+item.date+"</p>");
        if ( i == 3 ) return false;
    }); 
}, 'jsonp');
EAMann
I tried it without the extra set of paranthesis too, it did not work. Hence, I included the paranthesis so that it would call the function execute. Either way, it did not work.
rookie
A: 

Im going to go out on a limb and guess that (based on the url 'http://www.worldweatheronline.com/feed/weather.ashx?q=' ) that you are attempting to perform an AJAX request to an external site. This violates Same Domain Policy and will fail silently.

HurnsMobile
thats not the case, he is using jsonp
wowo_999
Whoops, I missed that!
HurnsMobile
+2  A: 

Turn on Fiddler or Firebug and watch the HTTP call go out. You'll see the HTTP error message there. Also, turn on your console in Chrome or Firefox to see a potential JavaScript error.

AndrewDotHay
Yeh, I tried that, but it did not show me any error messages.
rookie
Does it show that your request went out and came back with a successful response code?
AndrewDotHay
From the fact that the new information doesn't get appended to the body tag after submitting the request, I've noticed that the request doesn't seem to be satisfied. Although, I can't seem to point out what the logical error is.
rookie
This is what I get when I observe the request using firebug ---> jsonp1279132111667({ "data": { "request": [ {"query": "undefined", "type": "Unknown" } ] }})
rookie
+4  A: 

The problem you are experiencing is that you dont do anything to stop your form from being submitted, thus it submits the form before it gets a chance to get the data from the worldweatheronline.com.

Change your click handler to be like this:

    $(".button").click(function(event) { 
        event.preventDefault();

Here is my completed sample code that works the way you want it to:

<html>
<head>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'&gt;&lt;/script&gt;
<script type='text/javascript'>
    jQuery(document).ready(function() { 
        $('.error').hide();  
        $(".button").click(function(event) { 
            event.preventDefault();
            // validate and process form here
            $('.error').hide();  
            var zipCode = $("input#name").val();  
            if (zipCode == "") {  
                $("label#name_error").show();  
                $("input#name").focus();  
                return false;  
            }
            var key = 'ac9c073a8e025308101307';
            var noOfDays = 5;
            var url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=' + zipCode + '&format=json&num_of_days=' + noOfDays + '&key=' + key;

            alert('first'+url); 

            jQuery.get(url, function(test) { 
                alert(url); 
                $.each(test.data.weather, function(i, item){
                    $("body").append("<p>Date: "+item.date+"</p>");
                    if ( i == 3 ){
                        return false;
                    }
                }); 
            }, 'jsonp');
        });  
    });
</script>
</head>
<body>
<div id="contact_form">  
 <form name="contact" action="">  
   <fieldset>  
     <label for="name" id="name_label">Name</label>  
     <input type="text" name="name" id="name" size="30" value="" class="text-input" />  
     <label class="error" for="name" id="name_error">This field is required.</label>  

     <br />  
     <input type="submit" name="submit" class="button" id="submit_btn" value="Send" />  
   </fieldset>  
 </form>  
</div>  
</body>
</html>
wowo_999
This worked in terms of executing the second function partially. What I mean is that the second alert now pops up as well. However, the data from the web service still doesn't show up. Does this have to do with appending the information to the body tag? I can't seem to figure that out. Thank you for your help!
rookie
When I run the script as shown above, I see a series of date paragraphs appended below the form?
wowo_999