-2

Why does

$.get("/some-url", function(response-data){ #do something with response-data });

work in my project

but

$.get({
    url: "/some-url",
    success: function() {
        #do something with response data
    },
});

give me a bad request error...?

As far as I can tell, when I use url: the string is being converted into an object or something. The request is sent to localhost:3000/object%20Object, while when I use the first way it gets routed correctly.

What am I missing?! This is blowing my mind. Thanks!

Guilherme Franco
  • 1,465
  • 12
  • 19
  • 1
    because `$.get` doesn't support that syntax. did you mean `$.ajax` for the second sample? – Kevin B Feb 21 '14 at 15:40
  • `$.get()` isn't defined to expect an `Object` -- [***`url`** Type: String*](http://api.jquery.com/jQuery.get/). Only [`$.ajax()`](http://api.jquery.com/jQuery.ajax/) supports that with [***`settings`** Type: PlainObject*](http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings). – Jonathan Lonowski Feb 21 '14 at 15:43
  • You're not the only one, I was just about to ask the same question when I found this :D – John U Feb 24 '14 at 13:23

3 Answers3

4

You are passing settings paramters like $.ajax, remove {} from get({})

From jQuery Documentaion

jQuery.get( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )

$.get(
     "/some-url",{},
    function(resultData) {
        // do something with response data
    }
);
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
0

Change $.get({ to $.ajax({

$.ajax({
    url: "/some-url",
    success: function(data) {
        #do something with response data
    }
});
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

Forget $.get() and $.post() altogether, they are merely wrappers for $.ajax() and simply set type:'GET' or type:'POST' for you, so learning $.ajax() is by far the most useful.

Check out my answer here

ajax and php to enter multiple forms input to database

Community
  • 1
  • 1
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77