First dev
First dev

Reputation: 554

Using post method in ajax


I want to create a function use the AJAX but when I use the POST method to send the information the function don't work but the GET method is working
This is my function:

function ajax(page,method,id,send,a){
    a = a || true;
    page = page || '';
    method = method || 'GET';
    id = id || '';
    send = send || '';
    
    if(page!='' || id!=''){
    var xmlhttp;
    if(window.XMLHttpRequest)
    xmlhttp = new XMLHttpRequest();
    else
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     if(!xmlhttp)
   xmlhttp = new ActiveXObject("Mmxl2.XMLHTTP.3.0");
   if(!xmlhttp)
   xmlhttp = new ActiveXObject("Mmxl2.XMLHTTP.6.0");
    
        xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readystate == 4 || xmlhttp.status == 200)
        document.getElementById(id).innerHTML = xmlhttp.responseText;
        };
        
    if(send!=''){
    if(method=='get' || method=='GET'){
     xmlhttp.open("GET",page+'?'+send,a);
     xmlhttp.send();
    }
   else if(method=='post' || method=='POST'){
     xmlhttp.open('POST',page,a);
     xmlhttp.SetRequestHeader('Conect-Type','application/x-www-form-urlencoded');
     xhttp.send(send);
    }
    }
    }
} 

Upvotes: 0

Views: 179

Answers (2)

Mathias
Mathias

Reputation: 207

you could just use the already made jquery ajax calls for post and get

https://api.jquery.com/jquery.post/

$.post( "test.php", { name: "John", time: "2pm" })
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

https://api.jquery.com/jquery.get/

$.get( "test.cgi", { name: "John", time: "2pm" } )
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

Upvotes: 1

OddBrew
OddBrew

Reputation: 498

Please paste in your question the content of your fiddle, as links can go down. What is wrong for me in your code is this line :

xmlhttp.SetRequestHeader('Conect-Type','application/x-www-form-urlencoded');

'Conect-Type' should be replaced by 'Content-Type'.

Upvotes: 1

Related Questions