I'm trying to make an AJAX call to my web server to pull down information about a city.
.ajax()
is fired to "/data/[city]"alert()
with a JSON object it retrieved.Code:
$( document ).ready(function() {
$('#city').change(function () {
var city = $.trim($(this).val()).replace(/ /g,"-");
if(city.length > 0)
{
var url = "/data/cities/" + city;
$.ajax({
url: url,
context: document.body,
dataType: 'json'
}).done(function(data) {
alert(data);
});
}
});
});
For some reason, the .ajax()
call refuses to fire on occasion. The outgoing HTTP request is never made when url
equals:
/data/cities/New-York,-NY
However, it fires successfully when url
equals:
/data/cities/New-York-NY
(no comma)/data/cities/New-York,NY
(no dash)/data/cities/New-York,-NYC
(additional character)/data/cities/Ann-Arbor,-MI
(similar format)I have verified:
Am I missing something obvious? This seems really weird that .ajax()
wouldn't fire for this one specific URL...
EDIT:
Added an error callback to the .ajax()
request. It is failing with an error of state = "rejected" and statusText = "error".
As requested, the <select>
tag on the page:
<select id="city">
<option value=""></option>
<option value="New York, NY">New York, NY</option>
<option value="Ann Arbor, MI">Ann Arbor, MI</option>
</select>
By default, unless you used $.ajaxSetup for default settings you're not showing us, you're using the GET method. As you're not specifying an explicit value for cache, and its default value is true according to jquery spec (api.jquery.com/jQuery.ajax), your browser might still be working with an earlier (cached) response (even if the source code you see in the browser is up-to-date). I'd suggest using POST (if you an and if the server allows) and/or specifying cache: false explicity in the $.ajax({...}) call, just to rule any funny stuff out.
(copy/pasted from my comment as requested by the asker of the question)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With