简体   繁体   中英

converting XMLHTTPRequest To jquery ajax

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

Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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