Home > Enterprise >  converting XMLHTTPRequest To jquery ajax
converting XMLHTTPRequest To jquery ajax

Time:05-20

I try lots of ways to convert my XMLHTTPRequest into jQuery ajax but I couldn't be successful.

I would be really appreciate if anyone could help me solve this issue.

here is my XMLHttpRequest Code:

    var url = url;
    var xhr = new XMLHttpRequest();
    xhr.open("POST", url);    
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
    xhr.onreadystatechange = function () {
       if (xhr.readyState === 4) {
           console.log(xhr.status);
           console.log(xhr.responseText);
    }};
    
    var data = "ajax=1";  
    xhr.send(data); 

and here is my attempt but its not working

$.ajax({
             url: "<?php echo $this->ui_url('onlineexamcats', 'all'); ?>",
             data: JSON.stringify("ajax=1"),
             type: "POST",
             processData: false,
             contentType: false,
             beforeSend: function(xhr){xhr.setRequestHeader('Content-Type',  "application/x-www-form-urlencoded");},
             success: function(res) { console.log(res); }
          });

CodePudding user response:

Try this

$.ajax({
    url: "<?php echo $this->ui_url('onlineexamcats', 'all'); ?>",
    data: "ajax=1",
    type: "POST",
    processData: false,
    success: function(res) { console.log(res); }
});
  • Related