本文介绍了如何在没有jQuery的情况下重新编写代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用jQuery的情况下重写此代码?我需要在移动应用程序中执行此操作,我无法使用jQuery。

How can I rewrite this code without using jQuery? I need to do it in a mobile app where I can't use jQuery.

 $.ajax({
            type: "POST",
            url:"../REST/session.aspx?_method=put",
            data: JSON.stringify(dataObject, null,4),
            cache: false,
            dataType: "json",
            success: onSuccessLogin,
            error: function (xhr, ajaxOptions){
                    alert(xhr.status + " : " + xhr.statusText);
                }

        });


推荐答案

你可以试试这个:

var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
    // What you want to do on failure
    alert(xmlhttp.status + " : " + xmlhttp.responseText);
  }
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    // What you want to do on success
    onSuccessLogin();
  }
}

xmlhttp.open("POST", "../REST/session.aspx?_method=put", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Cache-Control", "no-cache"); // For no cache
xmlhttp.send("data=" + JSON.stringify(dataObject, null,4));

至于如何在没有jQuery库的情况下只用javascript做更多东西看看或

As for how to do more stuff with only javascript without jQuery library take a look at W3Schools AJAX Tutorial or Mozilla - Using XMLHttpRequest

正如duri所说,你将不得不找到一种方法将dataObject转换为字符串,而不是全部浏览器支持JSON对象。

And as duri said, you will have to find a way to convert dataObject to string, as not all browsers support JSON object.

这篇关于如何在没有jQuery的情况下重新编写代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:21