Philip Stuyck Philip Stuyck - 4 months ago 5x
Ajax Question

getjson does not work on production server but works on development machine

I have following javascript that is using a selection changed to fill in a select list.

$(function () {
$("#bedrijvenauto").each(function () {
var target = $(this);
var dest = target.attr("data-autocomplete-destination");
source: target.attr("data-autocomplete-source"),
select: function (event, ui) {
alert('selected bedrijf');
alert('selected bedrijf');
$.getJSON("/Project/GetListViaJson", { bedrijf: $('#BEDRIJF_ID').val() }, function (data) {
alert('selected bedrijf');
$("#PROJECT_ID").append(new Option("Maak een selectie", 0));
for (var i = 0; i < data.length; ++i) {
alert(data[i].value + ' ' + data[i].label);
$("#PROJECT_ID").append(new Option(data[i].label, data[i].value));
focus: function (event, ui) {

It works like a charm on my development pc. The alert are all coming out even the data is returning results. That is the difference with the development pc that does not give any results after the call to getJSON
I have the feeling I am missing a detail here.
I am not used to debugging on a webserver because I usually create GUI applications in WPF, and this is a student's work for his vacation and I now got to get it working without him being around anymore. Vacation is done :-(
But not for me.


The 404 error indicated in your comments means the url your creating is incorrect. Always make use of the @Url.Action() method to ensure they are correctly generated. In your script

var url = '@Url.Action("GetListViaJson", "Project")';
$.getJSON(url, { bedrijf: $('#BEDRIJF_ID').val() }, function (data) {

or if this is an external script, then add the var url = '@Url.Action(...)'; in the main view (razor code is not evaluated in external script files), or add it as a data- attribute to the element your handling

data-url = "@Url.Action(...)"

and get it again using var url = $(someElement).data('url');