2

Greetings from some noob trying to learn JQuery,

I am attempting to make it when you type something in a box below a div layer it reloads that layer upon submission of the form with a php get of the text box in the form. Expected behavior is it would reload that box, actual behavior is it don't do anything. Can someone help me out here.... Below is the code.

<div id="currentwxdiv">This is where the new stuff happens
</div>
<form name="changewx" action="/">
<input type="text" id="city">
 <input type="submit" name="submit" class="button" id="submit_btn" value="New City" /> 
</form>
<script>
  /* attach a submit handler to the form */
  $('form[name="changewx"]').submit(function(event) {


    /* get some values from elements on the page: */
    var $form = $( this ),
        city = $('#city').val()

    /* Send the data using post and put the results in a div */
    $('#currentwxdiv').load('http://api.mesodiscussion.com/?location=' + city);
    return false;

  });
</script>

Its giving the Javascript Console Error Error....

"XMLHttpRequest cannot load http://api.mesodiscussion.com/?location=goodjob. Origin http://weatherofoss.com is not allowed by Access-Control-Allow-Origin."

4
  • 1
    Are you getting any errors in the console? Commented Feb 4, 2012 at 14:38
  • 1
    This may not solve your problem, but the page you are 'post'ing data to is a directory, not a script. You may want to add either zones.php, current.php, embed.php or untitled.php Commented Feb 4, 2012 at 14:52
  • @Ram - I added the current.php - that was an error. Thanks for pointing that out. However it did not solve the problem. It still seems to be doing nothing. Commented Feb 4, 2012 at 15:31
  • @RoryMcCrossan XMLHttpRequest cannot load api.mesodiscussion.com/?location=goodjob. Origin weatherofoss.com is not allowed by Access-Control-Allow-Origin. Commented Feb 4, 2012 at 15:34

3 Answers 3

1

You are using POST method? is impossible to post to an external url because with ajax, the url fails the "Same Origin POlice".

If you use GET method, is possible to do that.

Another solution is to make a proxy. A little script that recive the params and then... using CURL or another thing you have to post to the external URL... finally, you jquery have to do the post thing to the proxy:

For example:

$.ajax({
  url: '/proxy.php?location=' + city,
  success: function(data) {
     $('#currentwxdiv').html(data);

  }
});

Sign up to request clarification or add additional context in comments.

Comments

0

I do it so:

    <div id="currentwxdiv">This is where the new stuff happens
    </div>
    <form name="changewx" action="/">
    <input type="text" id="city">

    </form>
    <script>

$('#city').keyup(function() {
var city = $('#city').val() 

$.ajax({
  url: 'http://api.mesodiscussion.com/?location=' + city,
  success: function(data) {
     $('#currentwxdiv').html(data);

  }
});



});

    </script>

2 Comments

I would try adding to the ajax function. data: "&location=" +city and removing it from post url
try it: var city = $('#city').val();
0

To help you out, i need to test this. What's the url address of your html code working ?

http://api.mesodiscussion.com/?location= doesn't work... only list the directory content... maybe that's de problem?

Greatings.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.