0

私はwpのajax関数を実行しています。しかし、私は常に応答0を受け取ります。ファイルadmin-ajax.phpのコードが表示され、次のように表示されます。

if ( empty( $_REQUEST['action'] ) )
    die( '0' );

これは私のjs関数ajaxです。

function fnc(){

            var ajax=new  XMLHttpRequest();
            ajax.open("POST", "<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php");



                ajax.onreadystatechange= function(){
                    if (ajax.readyState === 4) {
                        if (ajax.status === 200) {
                            alert(ajax.responseType);
                            alert(ajax.responseText);
                        } else {
                            alert('There was a problem with the request.');
                        }
                    }
                }
                ajax.send("action=some_function");


        }
4

3 に答える 3

3

文字列をフォームデータとして使用するにsendは、おそらく次のヘッダーを追加する必要があります。

ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

これがないと、PHPは生のPOSTデータを$_POST/$_REQUEST変数に変換しません。

于 2013-01-25T16:43:07.600 に答える
-1
If you want to use javascript and XMLHttpRequest this is the full way to do that :)


function ajax_post(){
// Create our XMLHttpRequest object
var ajax=new  XMLHttpRequest();

// Create data to send to our PHP file

var url = "xyz.php";
var fn = document.getElementById("a").value;
var ln = document.getElementById("b").value;
var variable = fn+" hello "+ln;
hr.open("POST", url, true);


// Set content type header  for sending url encoded variables

ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

// Get the onreadystatechange event for the XMLHttpRequest
ajax.onreadystatechange = function() {
    if(ajax.readyState == 4 && ajax.status == 200) {
        var return_data = ajax.responseText;
            alert(ajax.return_data);
            // Send the data to PHP now... and wait for response to update the status div
           ajax.send(variable); // Actually execute the request    
    }
}



}
于 2013-01-25T16:55:17.430 に答える
-1
$.ajax({
          type:'POST',
          url:"<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php",
          data:'', //  what you want to post
          success:function(data){
                    alert(data);
              });
             }
          });
        }

これを試して

于 2013-01-25T16:40:46.093 に答える