2013-02-21 85 views
1

我之前在AJAX中使用jQuery來傳遞REST服務中的標頭值。 標題值在Internet Explorer 8中傳遞良好。但在Firefox中不傳遞標題值,並且不調用該服務。
jQuery AJAX beforeSend在Firefox瀏覽器中失敗

這裏是我的代碼:

var postCall = function() { 
$.support.cors = true; 
var HFAssociateRefId = document.getElementById('MainContent_HFAssociateRefId').value; 
var Input = { 
    AssociateRefId: HFAssociateRefId 
};  
alert(JSON.stringify(Input)); 
var url = document.URL; 
var currentdate = new Date(); 
var datetime = (currentdate.getMonth() + 1) + "/" 
+ currentdate.getDate() + "/" 
+ currentdate.getFullYear() + " " 
+ currentdate.getHours() + ":" 
+ currentdate.getMinutes() + ":" 
+ currentdate.getSeconds(); 
$.ajax({ 
     type: "POST", 
     headers: { Accept: "text/plain; charset=utf-8", "Content-Type": "text/plain; charset=utf-8" 
       }, 
     beforeSend: function (xhr, settings) { 
     xhr.setRequestHeader("Date", datetime); 
     xhr.setRequestHeader("URL", url); 
       }, 
     url: "http://localhost:40680/LinkService.svc/TokenInsertion", 
     data: JSON.stringify(Input), 
     contentType: "application/json", 
     dataType: "json", 
     success: function (response) { 
     alert(response); 
       }, 
     error: function (xhr, status, error) {   
     alert(status); 
       },    
}); 

我也如本link指定調用XHR新的XMLHttpRequest嘗試。 但它不適用於Firefox。 ??
在此先感謝。

+0

在'beforeSend函數'中添加一個提醒並測試它,它工作與否? – 2013-02-21 05:38:06

+0

@Rohan Kumar是的,警報值發生在beforeSend函數中。但是標題值不會被傳遞。 – kk1076 2013-02-21 05:56:27

+0

如果它不起作用,那麼你可以在數據中傳遞'data和url' – 2013-02-21 06:22:18

回答

0

請參閱link。 Firefox和Chrome從不接受新的自定義標題,因爲它違反了瀏覽器安全規則。

0

拳頭的問題是這樣的:

type: "post", 
data: JSON.stringify(Input), <=== this is fail 
correct is: data:{data:JSON.stringify(Input)}, 
recuest to $_POST['data']; is better if using arrays... 

我建議你嘗試使用jQuery ...這個例子是很容易的。

基本樣本!

1到HTML或PHP不是很重要的這個名字...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Prueba Array 1</title> 
**<!-- important download JQUERY 1.3 or late... -->** 
    <script type="text/javascript" src="js/jquery-1.3.min.js"></script> 
    <script> 
    $(document).ready(function(){ 
    function toSend(d) { 
     $.ajax({ 
      type: "get", **// this is posible chain by "post"** 
      url: 'test.php', 
      data: {datos:JSON.stringify(d)}, 
      dataType: "json", 
      success: function(test) { 
       //console.log("visualizacion del registro Name[1]: "+ test.names[1]) 
       console.info("Array Send"); 

       $.each(test, function(key, value) { 
         console.log(key + ": " + value); 
         }); 
      } 
     }); 


    } 

    // send Submit 
      $(":send").live('click',function(){ 
            console.log("preparing to send! ");    
          // lista de entradas en un ID 
          var data = $('#form_1 input').serializeArray(); 
         // Serializacion de las entradas input 
    //     // Acciones 
        toSend(data); 

        }); 


    }); 
    </script> 
    </head> 

    <body> 
    <div id="ie"> 
    <form id="form_1" action="#" > 
    <input name="Nombre" type="text" value="Petronila"> 
    <input name="Apellido" type="text" value="Sinforosa"> 
    <input name="Telefono" type="text" value="phone"> 
    <input name="Ciudad" type="text" value="Living in Caracas"> 
    <input type="submit" value="Send"> 
    </form> 

    </div> 

    </body> 
    </html> 

下一個,複製這些代碼,然後將其保存爲test.php的!並運行

<?php 
if(isset($_GET['datos'])){ 
$names = array('john doe', 'JANE doe'); 
$ids = array('123', $datos); 

$data['names'] = $names; 
$data['ids'] = $ids; 


    echo json_encode($data); 
}else{ 
    $error = array('error','No se recibieron metodos'); 
    $error = array('messeng','Method need chain test to Get'); 
    echo json_encode($error); 
} 
?> 

確定是非常檢查在瀏覽器控制檯F12的結果來檢查任何這些非常重要的,需要激活控制檯的Firefox有時 IM測試就行了!

+0

使用數據:{data:JSON.stringify(Input)}在IE8和Firefox中都拋出一個Bad請求 – kk1076 2013-02-21 10:00:36

1

它看起來像Firefox不尊重標題Date。它發送標題URL。我無法找到任何資源來解釋此行爲。

作爲解決方案,您可以將標題Date重命名爲其他內容。

Chrome也顯示相同的行爲。

在進一步調查中,它看起來像是根據standard的標準行爲。請參閱標題爲Terminate these steps if header is a case-insensitive match for one of the following headers:

的部分。this question中提出了相同的問題。

+0

我將日期變量改爲一個字符串。但它仍然通過標題值。 – kk1076 2013-02-21 09:59:50

+0

@ kk1076它通過或不通過 – 2013-02-21 10:06:10

+0

:對不起。它沒有通過。 – kk1076 2013-02-21 10:09:39

相關問題