$.ajax doesn't return the response, and can't because AJAX is
asyncronous. That's why you've got callbacks that execute once you've
got a response.
for what you're doing, just use:
$.post("/goods/detail/","id="+id, function(json){
//do something useful with the data
updateGoodsDetails(json);
}, 'json');
or:
$.post("/goods/detail/","id="+id, updateGoodsDetails, 'json');
David
Charles Liu wrote:
try to track down the json data,so you can:
1.alert(data); before return (if it doesn't work see the following)
2.change the POST method to GET from the server side and then input
all paras into browser and see what will you get.(if it doesn't work
see the following)
3.track down something from the server side script
2009/3/5 Adwin Wijaya <adwin.wij...@gmail.com
<mailto:adwin.wij...@gmail.com>>
nope, i want to return as json.
price was there because i was logging into console. just for
debugging :) and i will do the calculation after it return the value.
I tried using async:false
and the result still undefined
function getGoodsDetail(id){
$.ajax({
type: "POST",
url : "/goods/detail/",
data : "id="+id,
dataType: "json",
async:false,
success : function(data){
return data ;
}
});
}
var x = getGoodsDetail(10);
any idea ?
I think i need to have callback so that I can return the value ...
just like in $.post and $.get.
is it possible ?
thanks
On Mar 5, 9:06 am, James <james.gp....@gmail.com
<mailto:james.gp....@gmail.com>> wrote:
> But does it work with async set to false?
> The code should work correctly if you have async set to false.
> However, you cannot do this (return the JSON from the function)
> properly without that setting set to false.
>
> By the way, is it suppose to be:
> result = price;
> ? I don't see what you're doing with the price variable in there.
>
> On Mar 4, 3:43 pm, Charles Liu <charles.li...@gmail.com
<mailto:charles.li...@gmail.com>> wrote:
>
> > hi Adwin,
> > it is "undefined" because it runs before ajax is finished.
> > maybe you should try to move "return result" next to the
position of "result
> > = data"
>
> > let me know if it works
>
> > Charles
>
> > 2009/3/5 Adwin Wijaya <adwin.wij...@gmail.com
<mailto:adwin.wij...@gmail.com>>
>
> > > Hi,
>
> > > can i have callback function on the $.ajax ?
> > > I don't want to use async since it will block the browser
... so can i
> > > use callback instead ?
>
> > > I would like to have function that return json like this
>
> > > function getGoodsDetail(id,member){
> > > var result ;
> > > $.ajax({
> > > type: "POST",
> > > url : "/goods/detail/",
> > > data : "id="+id,
> > > dataType: "json",
> > > async:false,
> > > success : function(data){
> > > var price = 0 ;
> > > if(member == 'Y'){
> > > price = data.priceMember ;
> > > }else{
> > > price = data.priceNonMember;
> > > }
> > > // I want to return data to user
> > > result = data
> > > }
> > > });
> > > return result; // --> always undefined
> > > }
>
> > > so when I call getGoodsDetail(id, membership) it will return
json
> > > value ...