reset password
Author Message
qma
Posts: 7
Posted 12:15 May 27, 2014 |

Hi
1. The ajax method in jQuery api is that jQuery.ajax( url [, settings ] ). The argument url [, settings ] passed in the jQuery.ajax method looks strange to me. What does the comma mean? Why the comma is inside the []?

2.In our lecture example, we actually use this method in the API: jQuery.ajax( [settings ] ) right?  Does [settings] means attribute?
When we write our code, we write the way like:

$.ajax({
            url: "wos.json",
            cash: false,
            success: function(data){
                $("ul").empty();
                for(var i = 0; i < data.usernames.length; i++){                
                    $("ul").append("<li>" + data.usernames[i] +"</li>");
                }            
            }
        });

Why we do not write the code like:

$.ajax( [ {
            url: "wos.json",
            cash: false,
            success: function(data){
                $("ul").empty();
                for(var i = 0; i < data.usernames.length; i++){                
                    $("ul").append("<li>" + data.usernames[i] +"</li>");
                }            
            }
        } ] );

 

xytian
Posts: 70
Posted 12:58 May 27, 2014 |

The bracket in "jQuery.ajax( url [, settings ] )" means optional.

Inside { } is a JavaScript Object.  [ ] would mean an array.

Last edited by xytian at 13:01 May 27, 2014.
cysun
Posts: 2935
Posted 13:12 May 27, 2014 |

.ajax() supports two ways to specify the url. The first way is to specify it as the first argument of the function, followed by an optional settings object, i.e. .ajax(url [, settings]); the second way is to specify it as a property of the setting object, i.e. .ajax({ url: "...", ...}).