2013-02-21 88 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

相关问题