如何从xhrpost函数中检索响应数据并将该对象存储在dojo / store中?

时间:2013-12-06 13:26:42

标签: javascript dojo

我已使用RESTDojo创建了一个登录信息。我正在使用dojo xhrpost提交我的登录表单数据。提交通过onClick函数执行。响应从rest方法返回。如何将响应对象存储在诸如dojo/Memory的dojo / store中?这样我就可以在任何html页面中检索它并删除对象以便注销。

dojo.xhrPost({
    url: "http://localhost:8080/userservices/rest/rest/login",
    form: dojo.byId("formNode"),
    load: function(user,status) {

        if(status.xhr.status == 200) {
            alert(user); //---> which displays username from the response method in rest method
            // What code for could be here for storing that user as an object to dojo store or memory to access several pages and delete the object?
            window.location.href ="jobseekerdashboard.html";                                                
        }
    }
});

1 个答案:

答案 0 :(得分:2)

不推荐使用dojo.xhrPost。看看dojo/request/xhr

require(["dojo/request/xhr", "dojo/store/Memory"], function(xhr, Memory){
  xhr("http://localhost:8080/userservices/rest/rest/login", {
    method: "POST",
    data: dojo.byId("formNode")
  }).then(function(returnedData){
    new Memory({
      data: returnedData
    });
    window.location.href= "jobseekerdashboard.html";
  }, function(err){
    // Handle the error condition
  }
});

如果更改窗口位置,您将失去当前的环境。如果要更改页面位置,则必须在页面加载后生成新的ajax请求,否则您将不得不在会话数据中传递数据。