2016-04-30 27 views
0

我有一個$.ajax在php中運行文件並傳遞一個變量(電子郵件地址)。該文件非常好,但xhr.status0HTTP狀態碼0可能是跨站點腳本?

這是代碼:

$.ajax({ 
      type: 'POST', 
      url: 'file.php', 
      data: { email: destinatario }, 
      complete: function(xhr,status){ 
      $('#momentaneo, #force').remove(); 
      alert(xhr.status); 
       if(xhr.status === 200){ 
        alert('ok');       
       } 
       else{ 
        alert('no');       
       } 
      }    
     }); 

file.php,獨立xhr.status,做工精細而且將用戶添加到我的通訊。所以我不認爲這個文件是問題.. 它有「0755」權限文件,如果我用瀏覽器運行file.php的URL沒有問題。

的問題是,xhr.status0,我不明白爲什麼...... 我讀這可能是「跨站點腳本」,但我不知道,如果是不可能性在我的情況。我也試圖改變標籤formdiv但沒有。

我還添加了file.php

<?php 
include('wrapper.php'); 
$apikey = "76a21109637d7391d1e68e9680e6"; 
voxmail_init($apikey); 
voxmail_user_subscribe(array('mail' => $_POST['email'],'privacy' => 1),$_SERVER['REMOTE_ADDR']); 

$header = "Content-type: text/html"; 
header($header); 
print('<b>ok</b>'); 
?> 

voxmail是通訊服務的API(我敢肯定,101%,該API不是問題)

我希望你能幫幫我,謝謝了很多並對不起我的英文

回答

1

我認爲問題是,當status作爲完整回調中的第二個參數傳遞給您時,您正在檢查xhr.status。試試這個:

$.ajax({ 
    type: 'POST', 
    url: 'file.php', 
    data: { email: destinatario } 
}).always(function(){ 
    $('#momentaneo, #force').remove(); 
}).then(function(){ 
    console.log.apply(console, arguments); 
    alert('ok'); 
}, function(){ 
    console.warn.apply(console, arguments); 
    alert('no'); 
}); 

控制檯語句僅用於調試目的,可以刪除。

+0

控制檯中還有這個「Multiorigine blocked request(cross-origin):原始對應的標準不允許讀取遠程資源http://www.example.comfile.php。Motivo:header CORS」Access- Control-Allow-Origin「mancante」。 – Borja

+0

@Borja你能告訴我們你的服務器端代碼處理這個請求嗎?也許您是否將客戶端重定向到另一個URL以響應此請求? – idbehold

+0

@Borja沒關係,我看到你已經添加了服務器端代碼。 – idbehold