XMLHttpRequest对象状态代码为0

时间:2015-03-14 05:45:34

标签: javascript ajax xmlhttprequest

我有一个使用XMLHttpRequest对象的Javascript函数,但我一直返回的状态是0.我已经完成了很多搜索,而我所提出的是0是一个未定义的错误并且可能由无数原因引起。因此,我希望你们能在我的代码中发现错误。

function initiateIPP(ID, Token)
    {
        var POSTRequest = new XMLHttpRequest();
        POSTRequest.onreadystatechange = function ()
        {
            if (POSTRequest.readyState == 4)
            {
                if (POSTRequest.status == 200)
                {

                }
                else
                {
                    alert("An error has occured, response code = " + POSTRequest.status);
                }
            }
        }
        var parameters = "SessionId=" + ID + "&SST=" + Token;
        POSTRequest.open("POST", "https://demo.ippayments.com.au/access/index.aspx", true)
        POSTRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        POSTRequest.send(parameters)
        window.open("IPPPage.html");
    }

提前致谢。

编辑:我在代码中添加了withCredentials行,但这似乎没有什么区别。     var parameters =" SessionId =" + ID +"& SST =" +令牌;             POSTRequest.withCredentials = true;             POSTRequest.open(" POST"," https://demo.ippayments.com.au/access/index.aspx",true)             POSTRequest.setRequestHeader(" Content-type"," application / x-www-form-urlencoded")             POSTRequest.send(参数)             window.open(" IPPPage.html&#34);

2 个答案:

答案 0 :(得分:2)

https://demo.ippayments.com.au/access/index.aspx”这应与您的javascript属于同一个域,否则,您应该设置:

Access-Control-Allow-Origin: *

在目标网页的响应标题上获取工作。见http://enable-cors.org/

例如,您可以将以下内容放在目标服务器上的web.config中:

<configuration>
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>
</configuration>

答案 1 :(得分:0)

要与https://demo.ippayments.com.au/access/index.aspx的IPPayments页面正确集成,您需要使用Direct Post方法。不使用XMLHttpRequest对象,而是使用HTML表单提交ID和令牌。例如 -

<form action="https://demo.ippayments.com.au/access/index.aspx" method="post">
    <input type="hidden" name="SST" value="1243123123123123123"/>
    <input type="hidden" name="SessionID" value="53535434535345"/>
    <input type="submit" value="submit" />
</form>