weixin_33744854 2015-09-04 13:00 采纳率: 0%
浏览 43

JavaScript Ajax保存结果

I'm having a problem. I'm trying do ajax on my own, but I have problem with saving result from ajax call. I want it to do just like jQuery does, or similarly. So, I have a function called ajax, with I parameter, and this parameter is an object with properties like: method, url, async, data, and success ... When I call ajax function, I have no problem except success .. I want it just like jQuery (dont ask me why I dont want to use jQ). So I want this

ajax({
    method: "POST",
    url: "ajax.php",
    async: false,
    data: "name=something",
    success: function(result) {
         console.log(result);
    }
});

and I have a problem to save result to parameter in definition of ajax function, and here just use it.

Here's ajax.php

<?php
  $name = "The input is: " . $_POST['name'];
  return $name;
?>

Here's a definition of an ajax function:

var ajax = function (arg) {
    if (typeof arg.method !== "undefined" && typeof arg.url !== "undefined" && typeof arg.async !== "undefined" 
        && typeof arg.success !== "undefined" && typeof arg.data !== "undefined") {

        var xmlhttp, i = 0,
        versions = [
            "MSXML2.XmlHttp.6.0",
            "MSXML2.XmlHttp.5.0",   
            "MSXML2.XmlHttp.4.0",  
            "MSXML2.XmlHttp.3.0",   
            "MSXML2.XmlHttp.2.0",  
            "Microsoft.XmlHttp"
        ];

        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            for ( ; i < versions.length; i++) {
                try {
                    xmlhttp = new ActiveXObject(versions[i]); break;
                } catch (e) { }
            }
        }

        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == XMLHttpRequest.DONE) {
                if (xmlhttp.status == 200) {
                    /* ////////////////////////////////////////////
                                  HERE I HAVE PROBLEM
                     /////////////////////////////////////////////*/

                     // this is obviously wrong (I know it is)
                     arg.success = function (something) {
                           something = xmlhttp.responseText;
                     }
                } else if (xmlhttp.status == 400) {
                    console.log("There was an error 400");
                } else {
                    console.log("UNSUCCESSFUL");
                }
            }
        }

        xmlhttp.open(arg.method, arg.url, arg.async);
        xmlhttp.send(arg.data);
        console.log("Method: " + arg.method + "
URL: " + arg.url + "
Async: " + arg.async + "
Data: " + arg.data + "
");
    }
};

How to save a xmlhttp.responseText to arg.success function parameter in a way that I can use the parameter in ajax function calls? Should I use callbacks?

EDIT: Thanks, it works but, it only print "The input is: ". How can I fix it?

  • 写回答

3条回答 默认 最新

  • csdnceshi62 2015-09-04 13:16
    关注

    You want to call the method, not set it.

    arg.success(xmlhttp.responseText);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ssh登录页面的问题
  • ¥60 渗透一个指定银行app,拿到客户信息,需要什么级别
  • ¥50 关于在matlab上对曲柄摇杆机构上一点的运动学仿真
  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥15 stm32 串口通讯过程中的问题
  • ¥20 公众号如何实现点击超链接后自动发送文字
  • ¥15 用php隐藏类名和增加类名
  • ¥15 算法设计与分析课程的提问