我的帖子请求有什么问题?

时间:2012-05-05 07:20:15

标签: jquery ajax

我在jquery中有这个发布请求代码,它似乎发送请求并返回结果(我可以在fiddler中看到它)但不知何故在webapp中它会出错并发出空警报。怎么了?

var jqxhr =$.post("http://abhishek:9090/abc/login.action",
    { emailaddress:     e_add,
      projectid:    p_id },
      function(xml)
      {     
      /*not coming here, goes to error*/

          if($(xml).find('isSuccess').text()=="true")
          {
            sessiontoken=$(xml).find('sessiontoken').text();

            var formMainRef=document.createElement("form");
            formMainRef.action="http://abhishek:9090/abc/home.action";
            formMainRef.method="post";
            formMainRef.target="_self";
            formMainRef.id="launch";
            document.body.appendChild(formMainRef);

            var cfgemailField = document.createElement("input");
            cfgemailField.name="emailaddress";
            cfgemailField.type="hidden";
            cfgemailField.value=e_add;
            formMainRef.appendChild(cfgemailField);

            var cfgpidField = document.createElement("input");
            cfgpidField.name="projectid";
            cfgpidField.type="hidden";
            cfgpidField.value=p_id;
            formMainRef.appendChild(cfgpidField);

            var cfgstField = document.createElement("input");
            cfgstField.name="sessiontoken";
            cfgstField.type="hidden";
            cfgstField.value=sessiontoken;
            formMainRef.appendChild(cfgstField);                                

            setCookie("abcsessionid", sessiontoken , 1);
            setCookie("abcusername",e_add,1);

            formMainRef.submit();
          }
      }
)
.error(function() {
  if(jqxhr.responseText == 'INVALID_SESSION') {
    alert("Your Session has been timed out");
    window.location.replace("/abc/view/index.html"); 
  }else  {
    /*comes here, after sending request*/
    alert( jqxhr.responseText);
  }
});

login.action返回一个小的xml

<Response>
  <sessiontoken>4611686352224309486</sessiontoken>
  <isSuccess>true</isSuccess>
</Response>

1 个答案:

答案 0 :(得分:0)

由于浏览器可能会阻止跨域请求,因此包含域是一个问题。通过使用相对路径,您将双重确认您要发布到的脚本,驻留在本地服务器上,绕过任何潜在的安全限制