繁体   English   中英

如何使AJAX使用POST而不是GET

[英]how to make AJAX use POST and not GET

我在这里得到了这个工作代码:

$(document).ready(function(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }

    $('#response').hide();

    $('#addroom').click(function(){
        var url = "./scripts/addnew_room.php";

        var room_num = document.getElementById('rm_num').value;
        var room_type = document.getElementById('rm_type').value;
        var tosend = "&rm_num="+room_num+"&rm_type="+room_type;

        ajaxRequest.open("POST", url, true);
        ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        ajaxRequest.onreadystatechange = function(){

            if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
                var return_data = ajaxRequest.responseText;
                document.getElementById("response").innerHTML = return_data;    
            }
        }
        ajaxRequest.send(tosend);
        $('#response').show();
        document.getElementById("response").innerHTML = '<img src="./ajax-images/ajax-loader.gif">';
    });
});

有人可以告诉我为什么它不将变量发送到POST方法中,即使我将方法设置为POST,它仍然使用GET方法并破坏了我的页面!

你可以用这样的东西

$.POST("url.com", {name: "John", location: "Boston"}, function(){

})

POST()是一个jQuery函数

您也可以使用jquery ajax()函数

$.ajax({
type:"POST",
url:"url.php",
data: { name: "John", location: "Boston" },
}).done(function(data){
// this is very simple to get or post data
$("#response").html(data);
})

最干净的方法是使用jQuery的$.post()函数:

$(document).ready(function() {
    $('#response').hide();

    $('#addroom').click(function() {
        $('<img />', {src: 'ajax-images/ajax-loader.gif'}).appendTo('#response');

        $.post({
            url: 'scripts/addnew_room.php',
            data: {
                rm_num: $('#rm_num').val(),
                rm_type: $('#rm_type').val()
            },
            success: function(response) {
                $('#response').html(response);
            }
        });
    });
});

或类似的东西

ajax = $.ajax({
    type: "POST",
    url: "your_php_page.php",
    data: "a=1&b=2",
    success: function(msg){     
        //success handler
    },
    error: function(msg){
       //error handler
    }
});

尝试这个:

function test_form(){
    var cnt = $("#formid").serialize();
    $.ajax({
    method: 'POST',
    url: 'url to your function',
    data: cnt,
    success: function(msg){
        //required code
    }
    });
}

你也可以用这个

dataValues = "a=1&b=2";
$.post("your_php_page.php", dataValues, function(data){

},"json");

//在您的php文件“ your_php_page.php”中

$a = $_POST['a'];

//您也可以通过这种方式在json fomat中返回数据

$returnData = array();
echo json_encode($returnData); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM