1

REST サービスでヘッダー値を渡すために、AJAX jQuery で beforeSend を使用しました。ヘッダー値は Internet Explorer 8 で適切に渡されます。ただし、ヘッダー値は 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);
                },              
});

また、このリンクで指定されているように、xhr を新しい XMLHttpRequest として呼び出してみました。しかし、Firefoxでは機能しません。??
前もって感謝します。

4

3 に答える 3

1

Firefox がヘッダーを尊重していないようDateです。ヘッダーを送信していURLます。この動作を説明するリソースが見つかりません。

解決策として、ヘッダーの名前を別の名前に変更できDateます。

Chrome も同じ動作を示します。

さらに調査すると、標準の標準的な動作のように見えます。というタイトルのセクションの下を見てくださいTerminate these steps if header is a case-insensitive match for one of the following headers:

この質問でも同じことが尋ねられました。

于 2013-02-21T08:25:08.370 に答える
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 をアクティブにする必要がある場合があります。

于 2013-02-21T07:58:06.617 に答える