2013年9月4日星期三

ajax asynchronous request to return the problem of data

ajax asynchronous request data inaccessibility to success Why is this method ?
code is also very simple, enter some data, through sending a json js asynchronous request to return a data . The code is not returned data there is a problem , you look great God ?
 @RequestMapping({"getAstSingleWheelImg_bbs"+Constant.JSON})
  @ResponseBody
  public Result getImgUrl(HttpServletRequest request, Model model, WheelChart chart)throws ParseException{
String userName = request.getParameter("userName");
    System.out.println("userName:"+userName);
    String astroDate = request.getParameter("astroDate");
    String astroHour = request.getParameter("astroHour");
    String astroMin = request.getParameter("astroMin");
    //略过部分代码

    result.setSuccess(true);
    result.setDesc(show_imgUrl);
    System.out.println(show_imgUrl);
    return result;
}

The entire code execution is no problem , the result is correct , Result is a returned class .
js code:
	$.ajax({
type:"post",
contentType:"application/x-www-form-urlencoded",
cache:false,
url:request_url+"/getAstSingleWheelImg_bbs.jo",
data:a.serializeArray(),
dataType:"json",
timeout:200,
beforeSend: function () {
alert("正在处理请求,请稍后。。。。。");
},
success:function(result){
$("img_wheel").attr("src",result.desc);
alert(a.desc);
},
error:function(){
alert("服务器忙,请重试!");
}
});
}});

execution result is only within the method to jump to beforeSend and error method , the error in which it , however , the background does not appear abnormal , there is also need to set up anything ?
solved !
------ Solution ---------------------------------------- ----
   error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    },

------ Solution ------------------------------ --------------
json format is specified , jquery help you package , as pop 0,0, timeout is timed out , the timeout: 200 set up big points
------ For reference only ----------------------------------- ----
are you ok?
------ For reference only ------------------------- --------------
saying returns a class can do ? Return JSON bar
------ For reference only ----------------------------------- ----
response to Json string got it,
And js where you want var json = eval (result); turn down before they can result.desc
------ For reference only ----------- ----------------------------
error method has three parameters , XMLHttpRequest, textStatus, errorThrown, alert the next textStatus to see what is wrong
------ For reference only --------------------------------- ------
response to Json string got it,
And js where you want var json = eval (result); turn down before they can result.desc
------ For reference only ----------- ----------------------------

this is a plus in which it ? How to add ?
------ For reference only -------------------------------------- -

do not add this parameter , it will jump to the error has been years, coupled with the good, but not receiving data .
async: false,
------ For reference only ------------------------------ ---------
js java object can not be received , this are not used dwr .. manual package json formatted object can still drop
------ For reference only ---------------------------------------

but this is actually can receive the Result object .
Look at this, this can be received . Procedure was not changed .
------ For reference only ----- ----------------------------------
background can not return a Result object , you need to get the output stream , out . print ( your json data ) , and then foreground xxx
------ For reference only -------------------------- -------------
ajax request the return value type is json, your conduct in the servlet json conversion
------ For reference only ----- ----------------------------------
var a=$("#wheelImg");
$.ajax({
async:true,
type:"post",
contentType:"application/x-www-form-urlencoded",
cache:false,
url:request_url+"/getAstSingleWheelImg_bbs.jo",
data:a.serializeArray(),
dataType:"json",
timeout:200,
beforeSend:function () {
alert("正在处理请求,请稍后。。。。。");
},
success:function(result){
$("img_wheel").attr("src",result.desc);
alert("success");
},
error:function(XMLHttpRequest, textStatus, errorThrown){
alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
}
});

into beforeSend method, and enter the error method , pop 0,0, timeout three values ​​, what is this , coupled with async: true does not work , false too. And why synchronization requests can receive this Result of the JAVA class, ajax request to do so .
------ For reference only -------------------------------------- -

phrase awoke . The timeout set up big points on ok , you can still receive data . Thanks .
------ For reference only -------------------------------------- -
timeout: 200, so little ah default is 0 if no timeout .
------ For reference only -------------------------------------- -


asynchronous request returns Result object is received , this code is not wrong , so alert (textStatus) is a timeout.
So , just take a timeout to set the bigger the line. The default setting is 0 . Never timeout .

没有评论:

发表评论