简体   繁体   English

如何使用POST [不使用jquery]从AJAX JavaScript返回结果?

[英]how to return result from AJAX javascript using POST [not using jquery]?

function AJAX(url,data)
{
    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)
        {
            return xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST",url,true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send(data);
}

var a = AJAX('log.php','username=Leonard&password=asjdklaslkjdalskjdlaksjda');

When the function return it gives me "undefined". 当函数返回时,它给我“未定义”。

Your function is asynchronous - it doesn't wait for the AJAX call to complete before returning so there's no results to return. 您的函数是异步的-在返回之前,它不等待AJAX​​调用完成,因此没有返回结果。

Try this: 尝试这个:

function AJAX(url, data, callback, ctx)
{
    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)
        {
            callback.call(ctx, xmlhttp.responseText);
        }
    }
    xmlhttp.open("POST",url,true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send(data);
}

Note how you now have to supply a "callback function" that'll be invoked when the AJAX completes: 请注意,您现在必须提供一个“回调函数”,当AJAX完成时将被调用:

AJAX('log.php','username=Leonard&password=asjdklaslkjdalskjdlaksjda',
    function(result) {
        // do stuff with result
    }
);

EDIT the ctx parameter I added is optional - it's so that you can set what this will be when the callback is invoked. 编辑 ctx我添加的参数是可选的-它让你可以设置什么this被调用的回调时,会。 This (no pun intended) will be useful if the callback is a method of an object rather than a standalone function. 如果回调是对象的方法而不是独立的函数,则此方法(无双关语)将很有用。

When you returns xmlhttp.responseText , you're returning to the onreadystatechange property. returns xmlhttp.responseText ,您将返回到onreadystatechange属性。 Not very useful, is it? 不是很有用,是吗?

Instead, use the asynchronous behavior to do the following: 而是,使用异步行为执行以下操作:

function AJAX( url, data, callback ) {

    // Your stuff

    xmlhttp.onreadystatechange = function() {
        if ( xmlhttp.readyState === 4 ) {
            callback( xmlhttp.responseText )
        }
    }

    // Your other stuff

}

Then, you can use it like: 然后,您可以像这样使用它:

AJAX( url, data, function( responseText ) {
    // use responseText there
} )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找和执行/提取嵌入从使用jQuery AJAX结果的JavaScript - Finding and Executing / Extracting embedded JavaScript from AJAX result using jQuery 如何以非Ajax方式使用jQuery(或Javascript)从API调用获取JSON结果? - How to get JSON result from an API call using jQuery(or Javascript) in a non-Ajax way? 如何使用Javascript / jQuery进行JSON的非ajax POST? - How to do a non-ajax POST of JSON using Javascript/jQuery? 如何使用jquery,javascript,ajax将模态信息发布到api - how to post modal information to a api using jquery,javascript,ajax Ajax和JavaScript,如何在不使用JQuery的情况下POST数组*? - Ajax and JavaScript, how to POST array *without* using JQuery? 如何使用JQUERY使用AJAX将Javascript变量(从客户端)发布到PHP文件(服务器端) - How to POST a Javascript Variable (From Client Side) to PHP File (Server Side) with AJAX using JQUERY 如何使用JavaScript / jQuery在不使用AJAX的情况下获得表单返回? - How to get form return without AJAX using JavaScript/jQuery? 如何使用jquery从ajax结果(json对象)访问数据 - How to access data from ajax result (json object) using jquery 如何使用 async/await 返回 Ajax 结果? - How to return an Ajax result using async/await? 如何使用jQuery $ .post通过ajax将表单字段发布/发送回服务器 - How to post/send form fields return by ajax to the server using jQuery $.post
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM