AbuMariam
AbuMariam

Reputation: 3708

Why is jQuery.get() changing my URL?

I have the below code where I am simply getting a resource with an Ajax jQuery call..

 $.get({
            url: "http://jennifer-lawrence/mygirl/FreeTonight.php",
            type: "GET",
            dataType: "json",
            success: function(b) {
                a(b, b.signature)
            }
        })

When I fire this, it fails and when I look in the Network tab of Chrome dev tools, I see that the request is going out to..

 http://jennifer-lawrence/mygirl/[object%20Object]

and of course the server returns a 404.

But if I change the "$.get" in the above code to "$.ajax" then it works fine.

What's up with that??

Upvotes: 0

Views: 178

Answers (2)

Dan
Dan

Reputation: 11114

Check out the docs: https://api.jquery.com/jquery.get/

$.get does not take an object in the same way that $.ajax does. Likely because it is a shortcut to be able to quickly write $.get(url).

Per the docs your code should be:

 $.get("http://jennifer-lawrence/mygirl/FreeTonight.php", Null,
        function(b) {
            a(b, b.signature)
        }
,'json');

Or just change $.get to $.ajax, problem solved.

Actually

You can pass an object to $.get in version >=1.12 and >=2.2. So your example would work if you update your jQuery version to one of those. But type: 'GET' is completely redundant.

Upvotes: 1

Quentin
Quentin

Reputation: 944556

[object%20Object] is what you get when you take a plain object, convert it to a string, and then URL encode it.

http://jennifer-lawrence/mygirl/[object%20Object] looks like you are on http://jennifer-lawrence/mygirl/ and then try to access the relative URI [object%20Object]

You would get this if you passed { some: object } as if it was a string representation of a URL.


Now see the docs. There are two ways you can call get:

jQuery.get( url [, data ] [, success ] [, dataType ] ) version added 1.0

and

jQuery.get( [settings ] ) version added: 1.12/2.2

You are using the second method, but it is acting like you are using the first method.

You must be using an old version of jQuery which does not support the second method.

Either use the first method or upgrade jQuery.

Upvotes: 1

Related Questions