dojo.xhrpost获取状态代码为0

时间:2013-03-21 09:10:35

标签: ajax dojo

我正在提交使用dojo的xhrpost。我的应用程序在rails上运行ruby。在控制器中,我正在执行重定向。我将重定向的URL存储在响应头中。我访问客户端上的重定向URL,然后在客户端上加载重定向的URL。以下是代码
在视图中,使用dojo.xhrpost执行ajax提交

         function () {
         var form = dojo.byId("form_id");

         dojo.connect(form, "onsubmit", function(event){
          // Stop the submit event since we want to control form submission.
          dojo.stopEvent(event);

          var xhrArgs = {
            form: dojo.byId("form_id"),
            handleAs: "text",
            load: function(data, ioargs){
            //getting redirected url from response header
             var new_url = ioargs.xhr.getResponseHeader("new_url");

             //redirecting to the url  
             document.location.href = new_url;
            },
            error: function(response){
            //handling error
            }
         }

          //submitting for to action1 of controller
         var deferred = dojo.xhrPost(xhrArgs);
      });
    }


控制器代码

        def action1
          new_url = url_for(:controller=>"controller", :action => "action2")
          #passing the new URL as parameter in the redirection
          redirect_to :action2, :new_url => new_url
        end

        def action2
          #getting the new url from the params and saving it in respone header so that it can be accesses in client
          response.headers["new_url"] = params[:new_url]
        end


这在我当地的主机上运行良好。但是,当我把它放在我的服务器上时,它失败了 我将ioargs.xhr.status改为“0”。数据是“”。虽然表单已保存,但响应为空,并且没有设置响应标头。
请帮助。

1 个答案:

答案 0 :(得分:0)

我不得不稍微修改控制器。

def action1
  if request.xhr?
    render :json => new_url.to_json
  else
  #follow normal redirection
  redirect_to :action2
end

同样在ajax电话中

var xhrArgs = {
        form: dojo.byId("form_id"),
        handleAs: "json",
        load: function(data, ioargs){
        //getting redirected url from data 
         document.location.href = data;
        },
        error: function(response){
        //handling error
        }

基本上我发现以不正确的方式将整个HTML页面作为响应返回。
只应在响应中返回URL,重定向应该从View

完成