【问题标题】:Javascript array for AJAX POST sendAJAX POST 发送的 Javascript 数组
【发布时间】:2011-07-16 21:38:09
【问题描述】:

这是交易...我需要制作一个 AJAX 保存脚本。我有一个基于 php 构建的整个系统,每个操作都需要刷新......我正在尝试通过使用 AJAX 来最小化刷新次数......我似乎无法找到一种方法来无损发送 WYSIWYG 编辑器输出到 PHP 脚本...

  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;
        }
    }
}

虽然此脚本运行良好,但我似乎无法找到提供发送选项的数组的方式...语法是什么还是有什么我不知道的?

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

【问题讨论】:

    标签: html ajax arrays post send


    【解决方案1】:

    在 javascript 代码中创建 自定义参数,如下所示:

       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 中,您将获得参数名称为 content 的全部内容。

    【讨论】:

      【解决方案2】:

      我会考虑使用 jQuery 和它的 Ajax 库:

      http://api.jquery.com/jQuery.ajax/

      而不是做所有你想做的事:

      $.post({url: 'action.php',data: output,success: function() { /* do something here */ }});
      

      【讨论】:

      • 问题是数据字段...我如何准备 POST 数组以便 id 不会丢失数据...例如如果我想编写以下变量 a=1 和 b =33 和 c='šaize kaut kāda un vēl sazin žuķīļņšģīŗ'... 现在 a 和 b 将是完美的,但 c 将在某些时候被切断,很可能只丢失 š ā ē ž ķ 等等...我需要它包含所有可能的字符,而不是像现在这样忽略它们......
      • 我也建议使用 jQuery 或其他一些 Ajax 库。
      猜你喜欢
      • 1970-01-01
      • 2013-04-22
      • 2018-02-01
      • 2012-08-24
      • 2019-04-19
      • 2011-02-21
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      相关资源
      最近更新 更多