簡體   English   中英

跨域請求的簡單ajax POST

[英]simple ajax POST with cross domain request

我正在嘗試向后端程序員制作的API發送僅包含一個參數的簡單表單,他告訴我,我要做的就是使用ajax通過POST將一個參數發送到給定的URL。

問題是我得到一個沒有'Access-Control-Allow-Origin'標頭存在的錯誤。

我已經閱讀了這個問題,並試圖實現第一個答案的解決方案,但沒有成功。 當我通過hurl測試API時,沒有問題。

這是我用來發送表格的代碼

    $( document ).ready(function() {
        $('.btnEnviar').click(function(){
            $.ajax({
                type: 'POST',
                url: 'http://xxxxx.xxx/subscribers/subscribeEmail',
                datatype: 'jsonp',
                async: true,
                success:function(){
                    try{
                        alert("ok");
                    }catch (e){
                        alert(e);
                    }
                } 
            });                
        });
    });

形式如下:

<form class="newsletter" method="post" action="http://xxxxx.xxx/subscribers/subscribeEmail">
                    <input type="text" placeholder="mail here!" class="input" name="email">
                    <input type="submit" class="send pull-right hidden" value="Subscribe me!" placeholder="Subscribe me!">
                    <span class="btnEnviar"><i class="fa fa-envelope"></i></span>
                </form>

該過程中不應涉及任何PHP。

關於我可能做錯了什么的任何見解?

嘗試將crossDomain: truexhrFields: { withCredentials: true }到請求中:

$( document ).ready(function() {
    $('.btnEnviar').click(function(){
        $.ajax({
            type: 'POST',
            url: 'http://xxxxx.xxx/subscribers/subscribeEmail',
            datatype: 'jsonp',
            async: true,
            xhrFields: {
               withCredentials: true
            },
            crossDomain: true,
            success:function(){
                try{
                    alert("ok");
                }catch (e){
                    alert(e);
                }
            } 
        });                
    });
});

參見http://api.jquery.com/jquery.ajax/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM