xhr.send(params)-多个参数

时间:2018-07-15 10:52:15

标签: javascript php ajax

我正在尝试传递多个参数,但是不知道语法。.PLz帮助

function formSubmit() {

    var name = document.getElementById('name').value;
    var email = document.getElementById('email').value;
    var phone = document.getElementById('phone').value;
    var message = document.getElementById('message').value;

    // This is where the problem is:
    var params = "name="+name&"email="+email&"phone="+phone&"message="+message;

    var xhr = new XMLHttpRequest;
    xhr.open('POST' , 'ajax.php', true);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')

    xhr.onload = function() {
        if(this.status ==200) {
            console.log(this.responseText);
        }
    }
    xhr.send(params);
}

1 个答案:

答案 0 :(得分:0)

我看到bdalina和AamirR的正确说法。 但是我没有看到答案。 因此,我用(bdalina和AamirR)的好话语给其他困扰您和我的人。

function formSubmit() {
var name = document.getElementById('name').value;
var email = document.getElementById('email').value;
var phone = document.getElementById('phone').value;
var message = document.getElementById('message').value;

// This is where the problem is:
var params = "name="+name"&email="+email"&phone="+phone"&message="+message;
//for send more parameters them one use "$" in the next POST identifier.
// like "nameOfValue="+value1"&NameNextValue"+value2

var xhr = new XMLHttpRequest();
//XMLHttpRequest is a function so u must have () on end of function name. XMLHttpRequest()
xhr.open('POST' , 'ajax.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')

xhr.onload = function() {
    if(this.status ==200) {
        console.log(this.responseText);
    }
}
xhr.send(params);}