简体   繁体   English

jQuery跨域发布重定向

[英]jQuery cross domain post redirect

I'm trying to use jQuery to POST a form to chargify. 我正在尝试使用jQuery来发布表单以进行收费。 My "Net" tab shows a 302 redirect (in red indicating an error), but jQuery is throwing a 404 error. 我的“网络”标签显示302重定向(红色表示错误),但jQuery抛出404错误。 Is it possible to preform a x-domain, post, redirect request from the browser or will I need to use proxy? 是否可以从浏览器执行x域,发布,重定向请求,还是我需要使用代理?

$(function() {

    var endpoint = "https://api.chargify.com/api/v2/signups"

    $('#new_sub_form').on('submit', function(e){

        e.preventDefault()

        var jqxhr = $.ajax({
            type: "POST",
            url: endpoint,
            crossDomain:true,
            data: $('#new_sub_form').serialize(),
            success: function(data, textStatus, request){
                console.log(request.getResponseHeader('Location'));
            },
            error: function (request, textStatus, errorThrown) {
                console.log(request.getResponseHeader('Location')); // Returns null
            }
        })
    })//on('submit')
})//ready()

UPDATE (more info): So I realized the 302 was redirecting me to a page that didn't exist on my server. 更新(更多信息):因此我意识到302正在将我重定向到服务器上不存在的页面。 Unfortunately once i fixed this, I still have an issue. 不幸的是,一旦我解决了这个问题,我仍然有一个问题。 From what I can tell, i POST to chargify, they then send a 302 back to the browser with the URI I specified. 据我所知,我开机自检,然后用我指定的URI将302发送回浏览器。 This URI is located on my server (localhost for now). 此URI位于我的服务器(目前为localhost)上。 Once the user is redirected my server parses the response and returns JSON. 重定向用户后,我的服务器将解析响应并返回JSON。 I tested the Response Header location via copy and paste into another tab and works fine. 我通过复制并粘贴到另一个选项卡测试了响应标题的位置,并且工作正常。

Chargify is only offering https, while my localhost is http. Chargify只提供https,而我的本地主机是http。 Would this cause the error?! 这会导致错误吗? HTTP Response HTTP响应

Ran into to a very similar problem the other day. 前几天遇到了一个非常类似的问题。 However im using ASP.NET MVC4. 但是即时通讯使用ASP.NET MVC4。

Its not enough if you use crossDomain:true u also need to add, dataType: 'json or html depending on the return value', xhrFields: { withCredentials: true }, You will also need to add these headers "Access-Control-Allow-Origin:" http://yourdomain.net " , Access-Control-Allow-Credentials:true" and maybe "Access-Control-Allow-Methods:GET,POST" in your case aswell to your RESPONSE. 如果您使用crossDomain:true还不够,还需要添加, dataType: 'json or html depending on the return value', xhrFields: { withCredentials: true },您还需要添加以下标头“ Access-Control-Allow -Origin:“ http://yourdomain.net ”,Access-Control-Allow-Credentials:true,在您的情况下也可能是“ Access-Control-Allow-Methods:GET,POST”,以及您的RESPONSE。

Chargify Direct does not support ajax/CORS. Chargify Direct不支持ajax / CORS。

You should use a "transarent redirect", as they describe, which is basically a standard form post redirecting the user to Chargify, and they will redirect the user back again to the URL you specify in the payload. 正如他们所描述的,您应该使用“透明重定向”,这基本上是将用户重定向到Chargify的标准形式,并且它们会将用户再次重定向回您在有效负载中指定的URL。 This means the user will briefly leave your site and return back to it. 这意味着用户将短暂离开您的站点并返回到该站点。

<form method="post" action="https://api.chargify.com/api/v2/signups">
</form>

Docs: https://docs.chargify.com/chargify-direct-introduction 文件: https//docs.chargify.com/chargify-direct-introduction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM