Andrei Orlov
Andrei Orlov

Reputation: 908

JQuery ajax ignores url

I've got function tryGetTicket which must call ajax request at my loginUrl. But when I call this function, it ignores my ULR and send get request without loginUrl, ie I expect what request will send at http://example.com/sys/login but in reality http://example.com. What's wrong?

$(function(){
 var tryGetTicket =
            function (loginUrl, param)
            {
                var ticket = null;
                $.ajax({
                    url: loginUrl,
                    dataType: "json",
                    data: param,
                    success: function( response ) {
                        var result = null;
                        //do something
                        ticket = result;
                    }
                });
                return ticket;
            }       

        $("#importDialog").click (function(event) {
            event.preventDefault();
            var alfTicket = getCookie("alf_ticket");
            var ticket = tryGetTicket("/sys/login", $.param({"alf_ticket" : alfTicket}));
            ///...
            }   
        });

Answer: It work correctly. Thanks for the advices.

 $(function(){
 var tryGetTicket =
            function (loginUrl, param, callback)
            {
                var ticket = null;
                $.ajax({
                    url: loginUrl,
                    dataType: "json",
                    data: param,
                    success: function( response ) {
                        var result = null;
                        //do something
                        ticket = result;
                        callback(ticket);
                    }
                });
            }       

        $("#importDialog").click (function(event) {
            event.preventDefault();
            var alfTicket = getCookie("alf_ticket");
            var ticket = 
               tryGetTicket("/sys/login", 
                            $.param({"alf_ticket" : alfTicket}), 
                            function(){
                               ///...
                            });
            }   
        });

Upvotes: 0

Views: 863

Answers (3)

Sudip
Sudip

Reputation: 2051

Try this one

var Path =  "http://example.com";

now add this variable in url path, like

url: Path+loginUrl,

Upvotes: 0

Miha Rekar
Miha Rekar

Reputation: 1546

Maybe because of the tiket typo? :P

Also AJAX calls are asynchronous (thats what the first A is for) so you have the result in the success function and do something with it there.

So in the click function call tryGetTicket as you did now but move all that ///... stuff in the success function of the tryGetTicket or make a new function and call it out of that success function.

Upvotes: 1

wroniasty
wroniasty

Reputation: 8052

Your tryGetTicket will almost always return null, because the $.ajax call is asynchronous.

There are no tiket and result variables.

How are you checking which URL is actually called?

Upvotes: 1

Related Questions