简体   繁体   English

在纯JavaScript结构中,这等效于什么? 这会将值发送到服务器端

[英]What is the equivalent of this in a pure JavaScript structure? This sends values to the server side

This script is designed to get the values from the inputs and send it by ajax to x.php as POST data to be echo by x.php. 该脚本旨在从输入中获取值,并通过ajax将其作为POST数据发送到x.php,以供x.php回显。 This works perfectly but I want to convert 这工作完美,但我想转换

this jQuery structure to a pure JavaScript structure so in other words how can I send a basic JS Object contents as POST data by AJAX to be echo by x.php 将此jQuery结构转换为纯JavaScript结构,换句话说,我如何通过AJAX将基本的JS对象内容作为POST数据发送给x.php进行回显

jQuery structure(works*) jQuery结构(作品*)

index.php index.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

$(document).ready(function(){

  $('#send').click(function(){
//Var structure
var first_name= $('#first_name').val();
var last_name= $('#last_name').val();

//Data var
var data={
      first_name: first_name,
      last_name: last_name
}


//Success var
var success= function(response){
   $('.output-container').html(response);
}

//Request
    $.ajax({
      data: data,
      method: 'POST',
      url: 'x',
      success: success
    });
  });
});

</script>

<input id='first_name' type='text' value='John'>
<input id='last_name' type='text' value='Doe'>

<button id='send'>Send</button>

<div class='output-container'><div>

JavaScript structure(Failed attempt*) JavaScript结构(尝试失败*)

index.php index.php

<script>
document.addEventListener('DOMContentLoaded', function(){

var execute_sendAjax = document.getElementById('send');
  execute_sendAjax.addEventListener("click", executeAjax);

var xhr= new XMLHttpRequest();
xhr.onreadystatechange = function(){
    if(xhr.readyState === 4){
        document.getElementsByClassName('output-container')[0].innerHTML= xhr.responseText;
    }
};

function executeAjax(){

  //Var structrue
  var first_name=  document.getElementById('first_name').value;
  var last_name=  document.getElementById('last_name').value;

//Data var
var data={
  first_name: first_name,
  last_name: last_name
}

    xhr.open('POST','x');
    xhr.send();
}
});
</script>

<input id='first_name' type='text' value='John'>
<input id='last_name' type='text' value='Doe'>

<button id='send'>Send</button>

<div class='output-container'><div>

x.php x.php

<?php

$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];

?>

<h1><?php echo $first_name; ?></h1>
</h1><?php echo $last_name; ?></h1>

In the pure JS structure I don't know where to go from there that's where I'm getting stuck at. 在纯JS结构中,我不知道从那里去哪里就是我被困在哪里。

Recent findings UPDATED* 最新发现已更新*

After doing some more research( Link ) I found out that application/x-www-form-urlencoded is another way to encode as POST data but I had to avoid using a JS object because it does not work with a normal JS object. 经过更多研究( 链接 )后,我发现application / x-www-form-urlencoded是编码为POST数据的另一种方法,但是我不得不避免使用JS对象,因为它不适用于普通JS对象。 I'm still looking for a way that I can use as a JS object. 我仍在寻找一种可以用作JS对象的方法。

<script>
document.addEventListener('DOMContentLoaded', function(){

var execute_sendAjax = document.getElementById('send');
  execute_sendAjax.addEventListener("click", executeAjax);

var xhr= new XMLHttpRequest();
xhr.onreadystatechange = function(){

    if(xhr.readyState === 4){
        document.getElementsByClassName('output-container')[0].innerHTML= xhr.responseText;
    }
};

function executeAjax(){

  //Var structrue
  var first_name=  document.getElementById('first_name').value;
  var last_name=  document.getElementById('last_name').value;

//Data var
var data = "first_name="+first_name+"&last_name="+last_name;

/*var data={
  first_name: first_name,
  last_name: last_name
}*/

    xhr.open('POST','x');
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //<--I notice this makes it encoded to be used as POST data
    xhr.send(data);
}
});
</script>

<input id='first_name' type='text' value='John'>
<input id='last_name' type='text' value='Doe'>

<button id='send'>Send</button>

<div class='output-container'><div>

JQuery is marshaling the Javascript object into form data. jQuery将Javascript对象编组为表单数据。 There are lots of ways to do this, but I think the easiest way is to use a FormData object like so: 有很多方法可以做到这一点,但我认为最简单的方法是使用FormData对象,如下所示:

function executeAjax(){
    var data = new FormData();
    // FormData.append(name, value) - appends a new value onto an 
    //  existing key inside a FormData object, or adds the key if 
    //  it does not already exist.
    data.append("first_name", document.getElementById('first_name').value;);
    data.append("last_name", document.getElementById('last_name').value;


    xhr.open('POST','x.php');
    xhr.send(data);
}

Then you don't have to edit any PHP or set the content-type in the request header. 然后,您无需编辑任何PHP或在请求标头中设置content-type。

If you don't like the FormData object (for whatever reason): 如果您不喜欢FormData对象(无论出于何种原因):

function executeAjax(data) {
    var data = [
        "first_name="+encodeURIComponent(document.getElementById('first_name').value),
        "last_name="+encodeURIComponent(document.getElementById('last_name').value)
    ].join('&').replace(/%20/g, '+');

    xhr.open('POST', 'x.php');
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send(data);
}

Learn more here . 在这里了解更多。

Sorry was not at the computer before. 抱歉,以前不在电脑前。

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

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