【问题标题】:XMLHttpRequest object status code of 0XMLHttpRequest 对象状态码为 0
【发布时间】:2016-05-04 11:30:05
【问题描述】:

我有一个使用 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 参数 = "SessionId=" + ID + "&SST=" + Token; POSTRequest.withCredentials = true; POSTRequest.open("POST", "https://demo.ippayments.com.au/access/index.aspx", true) POSTRequest.setRequestHeader("内容类型", "application/x-www-form-urlencoded") POSTRequest.send(参数) window.open("IPPPage.html");

【问题讨论】:

  • 我确实遇到了不同来源的问题......这可能是问题,我不确定,但如果是,我不确定是否真的可行.也许我必须问 IPP 他们希望我如何向他们发送数据...
  • 如果您遇到跨源资源问题,您可能应该使用某些服务器运行,可能是 apache,或者另一种方法是将 --disable-web-security 标志设置到您的 chrome 浏览器...
  • @Rakesh_Kumar --disable-web-security 只会为他解决问题。
  • 没错,@gabeio 是正确的;这需要在不强迫人们摆弄浏览器中的设置的情况下工作。

标签: javascript ajax xmlhttprequest


【解决方案1】:

"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>

【讨论】:

  • 等一下,你是说我需要在其他页面的响应头中设置一些东西?如何? o_O
  • @MichaelSmith 你应该从你的服务器搜索设置响应头
  • @MichaelSmith 我看到你使用 aspx,所以我想你可以在 web.config 中设置。
  • 将其添加到 web.config 并没有帮助。 :(
  • 是的,那么问题是你需要告诉他们允许 AndyChen 所说的话。
【解决方案2】:

要与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>

【讨论】:

  • 我最终做的是在代码隐藏中做所有事情(这是一个 ASP.NET 应用程序/网站),只是将 SessionID 和 SST 编码为 GET 参数并以这种方式重定向到页面。
猜你喜欢
  • 1970-01-01
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
相关资源
最近更新 更多