Philip Stuyck
Philip Stuyck

Reputation: 7457

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");
    target.autocomplete({
        source: target.attr("data-autocomplete-source"),
        select: function (event, ui) {
            alert('selected bedrijf');
            event.preventDefault();
            target.val(ui.item.label);
            $(dest).val(ui.item.value);
            $("#projectenauto").val("");
            alert('selected bedrijf');
            alert($('#BEDRIJF_ID').val());
            $.getJSON("/Project/GetListViaJson", { bedrijf: $('#BEDRIJF_ID').val() }, function (data) {
                alert('selected bedrijf');
                alert(data);
                $("#PROJECT_ID").empty();
                $("#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) {
            event.preventDefault();
            target.val(ui.item.label);
        }
    });
    target.val($("#BEDRIJF_NAAM").val());
});

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.

Upvotes: 0

Views: 124

Answers (1)

user3559349
user3559349

Reputation:

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');

Upvotes: 1

Related Questions