“JavaScript Ajax Post mengirim objek” Kode Jawaban

Panggilan posting data AJAX di JavaScript

$.ajax({
   url: 'ajaxfile.php',
   type: 'post',
   data: {name:'yogesh',salary: 35000,email: '[email protected]'},
   success: function(response){

   }
});
Tame Teira

JavaScript Kirim Data Posting dengan AJAX

function makeRequest (method, url, data) {
  return new Promise(function (resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.onload = function () {
      if (this.status >= 200 && this.status < 300) {
        resolve(xhr.response);
      } else {
        reject({
          status: this.status,
          statusText: xhr.statusText
        });
      }
    };
    xhr.onerror = function () {
      reject({
        status: this.status,
        statusText: xhr.statusText
      });
    };
    if(method=="POST" && data){
        xhr.send(data);
    }else{
        xhr.send();
    }
  });
}

//POST example
var data={"person":"john","balance":1.23};
makeRequest('POST', "https://www.codegrepper.com/endpoint.php?param1=yoyoma",data).then(function(data){
              var results=JSON.parse(data);
});
Grepper

JavaScript Ajax Post mengirim objek

function postAjax(url, data, success) {    var params = typeof data == 'string' ? data : Object.keys(data).map(            function(k){ return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) }        ).join('&');
    var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");    xhr.open('POST', url);    xhr.onreadystatechange = function() {        if (xhr.readyState>3 && xhr.status==200) { success(xhr.responseText); }    };    xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');    xhr.send(params);    return xhr;}
// example requestpostAjax('http://foo.bar/', 'p1=1&p2=Hello+World', function(data){ console.log(data); });
// example request with data objectpostAjax('http://foo.bar/', { p1: 1, p2: 'Hello World' }, function(data){ console.log(data); });
Australian Spiny Anteater

Jawaban yang mirip dengan “JavaScript Ajax Post mengirim objek”

Pertanyaan yang mirip dengan “JavaScript Ajax Post mengirim objek”

Lebih banyak jawaban terkait untuk “JavaScript Ajax Post mengirim objek” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya