JavaScript数组的AJAX POST发送数组、JavaScript、POST、AJAX

2023-09-10 18:20:50 作者:因为太帅被判无妻

下面是处理...我需要一个AJAX保存脚本。我有建立在PHP的一个完整的系统,每一个动作需要一个刷新......我试图尽量减少使用AJAX刷新次数......我似乎无法找到一种方法如何发送一个所见即所得的编辑器输出不受损失PHP脚本...

Here is the deal... I need to make an AJAX save script. I have a whole system built on php and every action needs a refresh... I'm trying to minimize the refresh count by using AJAX ... I can't seem to find a way how to send a WYSIWYG editor output without loss to the PHP script...

  if (window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
}
else{
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
function save(){
    xmlhttp.open('POST','action.php',true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", document.getElementById('output').value.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(document.getElementById('output').value);
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status==200){
            $('#ajaxresult').css('opacity', 0.1);
            $('#ajaxresult').stopAll().pause(1000).fadeTo(400,1);
            $('#ajaxresult').stopAll().pause(3000).fadeTo(400,0, function(){$(this).hide();});
            document.getElementById('ajaxresult').innerHTML=xmlhttp.responseText;
        }
    }
}

虽然这个脚本工作正常,我似乎无法找到路什么样的数组给的发送选项...什么是语法,或者是有什么我不知道吗?

While this script works fine I can't seem to find the way what kind of array to give the send option... what is the syntax or is there something I don't know?

顺便说一句,我是初学者在JS ...

BTW I'm a beginner in JS...

推荐答案

创建自定义参数在JavaScript中$ C $象下面这样:C:

create custom parameter in the javascript code like below:

   var jspNameParam = "content="+escape(document.getElementById('output').value);
    function myFunction() {
        if (xmlhttp) {
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                /*  want to accsess some data written from action.php */
                }   
            };          
            xmlhttp.open("POST", "action.php", true);   
            xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlhttp.send(jspNameParam);
        }
    }

现在在action.php的你将得到全部内容与参数名内容

Now in action.php you will get whole content with the parameter name content.