Ajax - $.get data from server

I'm trying to get the city name based on the zip code given by user in an input field.

This input, and ajax function:

<input type="text" name="postal_code" id="postal_code" 
onkeyup="
  $.get('<?=$config['url']?>/ajax/location/?code='+this.value, 
  function(data){ $('#location').html(data); });" 
  maxlength="5" />

This is the /ajax/location/?code= file:

 <?php
    $city = mysql_fetch_object(mysql_query("SELECT * FROM postal_codes WHERE postal_code = '" . intval($_GET['code']) . "'")); 
 ?>
 <div>
   <p>
      <input value="<?php echo $city->city_name; ?>" name="city"  />
   </p>
</div>

I used .ajaxError to see the error, but it doesn't help. It alerts only the url and no information about the error.

$(document).ajaxError(function(e, xhr, settings, exception) {
            alert('error in: ' + settings.url + ' \n'+'error:\n' + xhr.responseText );
            }); 

This is what ajaxError alert:

error in: http://mywebsitename.com/ajax/location/?code=11824 
error:

I tried to get a .txt file using $.get and it worked. So I'm 100% sure that ajax is working properly.

Thanks.

Answers


the /ajax/location/?code= not have a db connection

EDIT:

try this:

$.get('<?=$config['url']?>/ajax/location/', {code:this.value}, function(data){ $('#location').html(data); });

Solved my problem.

The problem was:

<?=$config['url']?>

Absolute url. I removed that and it worked.

'/ajax/location/?code='+this.value

Thanks everyone for trying to help me!


Need Your Help

not getting sms broadcast on xiomi redmi device

android android-broadcast

I am working on a app which reads the otp code from incoming sms. I am using sms recieve broadcast to read the incoming sms. Its working fine on every device except xiomi redmi .

Initialization of a pointer to an array of int

c++ arrays pointers initialization int

I have been mucking around with C++ once again and noticed a strange behavior regarding the initialization of an array when declared as a pointer inside a class member method or inside the main()