admin管理员组文章数量:1394089
My first question here.
Has anybody had any problem with using shorthand functions for ajax requests?
This works:
('#book').typeahead({
source: function(typeahead, query){
return $.ajax({
url: "/book/autopleteBooks",
type: "GET",
dataType: "JSON",
data: {queryString: query},
success: function(results){
typeahead.process(results);
}
});
},
property: "title",
onselect: onSelectBook
});
But none of these two works:
('#book').typeahead({
source: function(typeahead, query){
return $.get({
url: "/book/autopleteBooks",
dataType: "JSON",
data: {queryString: query},
success: function(results){
typeahead.process(results);
}
});
},
property: "title",
onselect: onSelectBook
});
('#book').typeahead({
source: function(typeahead, query){
return $.getJSON({
url: "/book/autopleteBooks",
data: {queryString: query},
success: function(results){
typeahead.process(results);
}
});
},
property : "title",
onselect: onSelectBook
});
The other thing is that replacing url
with createLink
does not work also.
url: "/book/autopleteBooks"
url: "${createLink(controller: 'book', action: 'autopleteBooks')}"
I'd rather use shorthand functions to make to code to be simplier to read and basically for aesthetics :)
My first question here.
Has anybody had any problem with using shorthand functions for ajax requests?
This works:
('#book').typeahead({
source: function(typeahead, query){
return $.ajax({
url: "/book/autopleteBooks",
type: "GET",
dataType: "JSON",
data: {queryString: query},
success: function(results){
typeahead.process(results);
}
});
},
property: "title",
onselect: onSelectBook
});
But none of these two works:
('#book').typeahead({
source: function(typeahead, query){
return $.get({
url: "/book/autopleteBooks",
dataType: "JSON",
data: {queryString: query},
success: function(results){
typeahead.process(results);
}
});
},
property: "title",
onselect: onSelectBook
});
('#book').typeahead({
source: function(typeahead, query){
return $.getJSON({
url: "/book/autopleteBooks",
data: {queryString: query},
success: function(results){
typeahead.process(results);
}
});
},
property : "title",
onselect: onSelectBook
});
The other thing is that replacing url
with createLink
does not work also.
url: "/book/autopleteBooks"
url: "${createLink(controller: 'book', action: 'autopleteBooks')}"
I'd rather use shorthand functions to make to code to be simplier to read and basically for aesthetics :)
Share Improve this question edited Sep 15, 2012 at 8:11 Asciiom 9,9657 gold badges41 silver badges59 bronze badges asked Sep 15, 2012 at 8:01 mindfulnessmindfulness 411 silver badge4 bronze badges 1- What exactly doesn't work? Any error messages? Is the request actually being made? Is there a response? etc... – Ayman Safadi Commented Sep 15, 2012 at 8:10
3 Answers
Reset to default 8Structure of $.get()
is like:
$.get(
"/book/autopleteBooks", // url
{queryString: query}, // data
function(data) { // success
// code
},
'json' // dataType
);
and $.getJSON()
is:
$.getJSON(
"/book/autopleteBooks", // url
{queryString: query}, // data
function(results){ // success
// code
}
);
Read more about $.get() and $.getJSON()
Please read the documentation of the two shorthand methods you are trying to use. They don't accept an object of options as first parameter.
$.get() id shorthand for $.ajax() method. But actually the syantaxfor $.get is like this
$.get('ajax/test.html', function(data) {
$('.result').html(data);
alert('Load was performed.'); });
where a $.ajax() is called as
$.ajax({
url: url,
data: data,
success: success, dataType:dataType });
for more info on $.get() refer http://api.jquery./jQuery.get/
本文标签:
版权声明:本文标题:javascript - jQuery $.ajax function works, but shorthand functions like $.get$.getJSON do not - using jQuery 1.7.2 and Grails 2. 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744589461a2614398.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论