2017-07-01 18 views
1

私は非常に初心者ですが、最後の3時間はAJAX経由でデータを送信する問題に対処しようとしています。長い文字列をAJAX経由でスクリプトに送信しよう

文字列を関数を使用してPHPファイルに送信しようとしています。この関数の機能は、クエリを持ちHTMLを出力するPHPファイルにGETリクエストを送信することです。この関数はHTMLを取得し、それを "html_user_club"に配置します。

function userClubVotes(str_1,str_2,str_3) { 
     if (str_1 == "") { 
      document.getElementById("html_user_club").innerHTML = ""; 
      return; 
     } else { 
      if (window.XMLHttpRequest) { 
       // code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp = new XMLHttpRequest(); 
      } else { 
       // code for IE6, IE5 
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      xmlhttp.onreadystatechange = function() { 
       if (this.readyState == 4 && this.status == 200) { 
        document.getElementById("html_user_club").innerHTML = this.responseText; 
       } 
      }; 
      xmlhttp.open("GET",'ajax/ajax-user-club-votes.php?user_id='+str_1+'&user_to_visit='+str_2+'&following_votes='+str_3,true); 
      xmlhttp.send(); 
     } 
    } 

URLがあまり長くない場合にのみ動作します。このURLで、それは動作しません:

AJAX/AJAXユーザー・クラブvotes.phpのuser_id = 13 & user_to_visit = 134 & following_votes = 1323.1323.134.23425.25245.42.24524.233.2344.232.1323.134.23425.25245.42.24524? .233.2344.232.1323.134.23425.25245.42.24524.233.2344.232.1323.134.23425.25245.42.24524.233.2344.232.1323.134.23425.25245.42.24524.233.2344.232.23425.25245.42.24524.233.2344.232.1323.134.23425.25245.42.24524.233.2344.232.1323.134.23425.25245 .42.24524.233.2344.232.1323.134.23425.25245.42.24524.233.2344.232。

だから、AJAX経由で長いURLを送信する方法はありますか?

EDIT:

[OK]を、私はPOSTの代わりにGET経由して文字列を送信する必要があります。私はこれを変更しようとした:結果なし

var string = '?user_id='+str_1+'&user_to_visit='+str_2+'&following_votes='+str_3; 
var body = "string=" + encodeURIComponent(string); 
xmlhttp.open("POST", "ajax/ajax-user-club-votes.php", true); 
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
xmlhttp.setRequestHeader("Content-Length", body.length); 
xmlhttp.setRequestHeader("Connection", "close"); 
xmlhttp.send(body); 

しかし、それでもまだ:このため

xmlhttp.open("GET",'ajax/ajax-user-club-votes.php?user_id='+str_1+'&user_to_visit='+str_2+'&following_votes='+str_3,true); 
xmlhttp.send(); 

を。どんな手掛かり?

EDIT_2

別のポストを経由してGETと最後を介して第1の二つの変数、試してみてください。

xmlhttp.open("GET",'ajax/ajax-user-club-votes.php?user_id='+str_1+'&user_to_visit='+str_2,true); 
xmlhttp.send(); 
var body = "string=" + encodeURIComponent(str_3); 
xhr.open("POST", "ajax/ajax-user-club-votes.php", true); 
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
xhr.setRequestHeader("Content-Length", body.length); 
xhr.setRequestHeader("Connection", "close"); 
xhr.send(body); 

ない成功、しかし有望に見えます。何か案が? (私はあなたが代わりにPOSTリクエストを送信することができますSending a long string through ajax doesn't work

+0

クエリ文字列の制限は一般に2048文字です。最新のブラウザではさらに多くの処理が可能です。問題はあなたのサーバサイドのコードではないと確信していますか? – adeneo

+0

サーバサイドのコードは簡単で、 '$ _GET ['following_votes'];' できません! –

+0

POSTリクエストでは、送信しているデータから先行する '?'を削除します。 – CBroe

答えて

0

のように何かをしようとしています。だから、あなたはリクエストのボディにとの.phpファイルにfollowing_votesを渡すあなたが$_POST["following_votes"]として値を取得します。

+1

クライアント側のコードを変更するには? – charlietfl

+0

私はちょうどPOST経由で試して質問を更新しました... –

0

私にとっては、私はいつも だからjqueryの中で、あなたがあなたの問題を解決することができます... jQueryを使ってAjaxのリクエストを実行します。

function userClubVotes(str_1,str_2,str_3) { 
    if (str_1 == "") { 
     document.getElementById("html_user_club").innerHTML = ""; 
     return; 
    } else { 

     $.ajax({ 
      method:"POST", 
      url:"ajax/ajax-user-club-votes.php", 
      data:{ 
      user_id:str_1, 
      user_to_visit: str_2, 
      following_votes:str_3 
      }, 
      success:function(response){ 
       //handle response from the server 
      }, 
      error:function(){ 
      //handle error 
      } 
     }) 
    } 
} 

そして、PHP側では、あなただけを使用する必要があります。

<?php 
var_dump($_POST["user_id"]); 
var_dump($_POST["user_to_visit"]); 
var_dump($_POST["following_votes"]); 

データをスクリプトに投稿するには、 "POST"(すべての変数)を使用する方がよいでしょう。

+0

これはAJAX Jqueryを使用しています... Muchoはもっとクリアです!そして、私は私が文を入れなければならないと思います。\t \t \t \t \t 'document.getElementById(" html_user_club ")innerHTML = this.responseText;' success:function(response){'... –

+0

レスポンスはバックエンドによって送信されるテキストです。バックエンドがjsonオブジェクトを送信する場合、応答はオブジェクトです – thib3113

関連する問題