How do I POST multiple outputs with this ajax functions?

会有一股神秘感。 提交于 2020-01-17 03:49:07

问题


I have a function that send one output i.e. 'OPTIONS' as json using ajax. Now I need to POST multiple say two outputs. How do I modify this function so that I could POST two valiues. Here is my ajax function.

function ADDLISITEM(form)
    { 
    var options = form.txtInput.value;
    options = JSON.stringify(options);
    var url = "send_mysql.php"
    var request = null;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    request=new XMLHttpRequest();
     }
    else
    {// code for IE6, IE5
    request=new ActiveXObject("Microsoft.XMLHTTP");
    }
    request.open("POST", url, true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Connection", "close");
    request.onreadystatechange = function(){
        if (request.readyState == 4) {
            if (request.status == 200) {
                //alert('POST');
        } else {
            alert(request.status); // fails here
        }
        }
    }
    request.send("options=" + encodeURIComponent(options).replace(/%20/g, '+'));
    }
    </script>

回答1:


Add another parameter to the request.send call like this:

var someOtherValue = JSON.stringify({actual : "data", goes : "here"});
request.send("options=" + encodeURIComponent(options).replace(/%20/g, '+') + "&someOtherValue=" + encodeURIComponent(someOtherValue).replace(/%20/g, '+'));


来源:https://stackoverflow.com/questions/4308508/how-do-i-post-multiple-outputs-with-this-ajax-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!