Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript ajax post send an object

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); });
Comment

PREVIOUS NEXT
Code Example
Javascript :: es6 class example 
Javascript :: promise in js 
Javascript :: javascript continue with while Loop 
Javascript :: dom 
Javascript :: TypeError: Converting circular structure to JSON 
Javascript :: base64 from file 
Javascript :: js arrays in arrays 
Javascript :: instanceof 
Javascript :: javascript object as key 
Javascript :: polymer js tutorial 
Javascript :: javascript highlight element 
Javascript :: react faq 
Javascript :: resize js 
Javascript :: jquery OR operation 
Javascript :: Search by text score in mongodb 
Javascript :: call node js function from javascript 
Javascript :: console.log is not a function 
Javascript :: .has js 
Javascript :: JavaScript substr() Syntax 
Javascript :: async await map 
Javascript :: bind method in js 
Javascript :: run react native with debugger breakpoint 
Javascript :: react hooks useeffect 
Javascript :: replace() in javascript 
Javascript :: js string encode decode arabic 
Javascript :: typeahead bootstrap 4 add multiple values 
Javascript :: mongoose $in operator order not respected 
Javascript :: npm i react-router semantic-ui-react semantic-ui-css 
Javascript :: scriptmanager call javascript function 
Javascript :: jsrender get index 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =