user1938509
user1938509

Reputation: 455

JQuery ajax call - can't pass url parameter

Sorry for this "newbe" question.

Here is my problem:

I am using JQuery 1.8.2 and I am trying to issue an ajax request:

This is working:

    $.ajax({
        url: 'http://191.168.0.133/rest/interface/test/1',
        dataType: 'json',
        timeout: 5000,
        success: function(data, status){
                $.each(data, function(i,item){
                        var registrationInfo = '<h1>' + item.testID+ '</h1>';
                        output.append(registrationInfo);
                    });
                },
        error: function(){
                output.text('There was an error loading the data.');
            }
        });

But I want the URL Parameter to be passed by a variable. The folloing is not working, but always ending up in the error function "There was an error loading the data."

    var testURL = 'http://191.168.0.133/rest/interface/test/1';
    $.ajax({
        url: testURL,
        dataType: 'json',
        timeout: 5000,
        success: function(data, status){
                $.each(data, function(i,item){
                        var registrationInfo = '<h1>' + item.testID+ '</h1>';
                        output.append(registrationInfo);
                    });
                },
        error: function(){
                output.text('There was an error loading the data.');
            }
        });

I am going bonkers over this. Can anyone give me a hand? Thanks in advance ...

Upvotes: 1

Views: 2773

Answers (3)

Calvin
Calvin

Reputation: 41

I think adding a semicolon after the url will fix your issue.

If there was a semicolon then I'd suggest posting a jsfiddle and try if you can get it down to the bare differences that cause the error.

Upvotes: 4

user1938509
user1938509

Reputation: 455

I was able to work around the problem.

While the $.ajax command is still not working, i switched to $.getJSON and this works:

    var testURL = 'http://191.168.0.133/rest/interface/test/1';
    $.getJSON(testURL, function(data){
            $.each(data, function(i,item){
                    var registrationInfo = '<h1>' + item.testID+ '</h1>';
                    output.append(registrationInfo);
                });
            };

No clue still, why the $.ajax thing won't work, since getJSON is only a shorthand for the ajax command.

Upvotes: 0

webextensions.org
webextensions.org

Reputation: 739

The code that you have mentioned seems to be fine. I guess you might be ignoring something else ... anyways, just to guide you with a few suggestions:

  • You said it is not working ... Do you mean you are getting a syntax error? You can check it in the browser console. F12 key opens the console in most browsers. OR open the error console from the browser menu and see. Or does the error function execute

  • You might like to put a semi-colon at the end of each "statement" (like variable declaration, variable assignments) to avoid a beginner's confusions due to the "automatic semi-colon insertion" feature of JavaScript.

Upvotes: 0

Related Questions