Is it valid to define function(json) and if yes what does it mean? [closed]
$.getJSON(link,function(json)
{
if( json.length> 0)
{
document.form_reserve.action ="http://localhost/ReserveRoomsBackend.php?
bldg_number="+json[0].bldg_number +" & room_number="+json[0].room_number;
document.getElementById("form_reserve").style.display = "block";
}
else
{
开发者_如何学Go document.getElementById("label").style.display = "block";
}
alert("inside json finction" + json.length+" "+json[0].room_number+" "+json
[0].bldg_number);
}); //.getJSON
}//doAjaxPost
it's just an argument. The function expects ONE argument which is a JS object that jQuery has evaluated from a JSON string.
You're potentially confused by the fact that the {
is on the next line, but it's the same as:
function(whatever){
You are calling the function $.getJSON (part of jQuery I guess) with an anonymous function as second parameter/argument (as a callback in this situation). Those anonymous functions are like "normal" functions, they just don't have a name and since you pass this function to getJSON as a reference, jQuery is able to call your function. So, yes it's valid. If this is what you meant.
In this context, json is a parameter passed to a function object, so yeah it's fine (although it's not very representative of the objects content) - it could be named anything.
In this case it could be called "rooms", and you would could call rooms[0].room_number instead of json[0].room_number.
For example, in this case "message" is the name of the parameter:
showAlertDialog("This an example");
function showAlertDialog(message) {
alert("Your message was:" + message);
}
"json" is often used as the name for the response object in jQuery examples (as it is in your case here).
精彩评论