繁体   English   中英

通过Ajax请求将PHP数组转换为JavaScript数组

[英]PHP array to JavaScript array via Ajax request

我想要此Ajax请求的结果填充JavaScript hintArray 也就是说,我希望hintArray包含与$row相同的值。 可以用简单的方法完成吗? php文件应该如何输出? hintArray = JSON.parse(this); 正确?

非常感谢

的JavaScript

var hintArray = new Array();
postAjaxRequestFunktion(minFunktion, 'getHint.php', 'id =1')
function minFunktion()
{
  hintArray = JSON.parse(this); 

}

function postAjaxRequestFunktion(minFunk,minUrl, mittArg)
{
   var contenttype = 'application/x-www-form-urlencoded'
   var minRequest        = new skapaAjaxObjekt(minFunk)
   if (!minRequest) return false
   minRequest.open('POST', minUrl, true)
   minRequest.setRequestHeader('Content-type',   contenttype)
   minRequest.setRequestHeader('Content-length', mittArg.length)
   minRequest.setRequestHeader('Connection',     'close')
   minRequest.send(mittArg)
   return true
}

function skapaAjaxObjekt(minFunk)
{
   try       { var   minRequest = new XMLHttpRequest()                   }
   catch(e1) { try { minRequest = new ActiveXObject("Msxml2.XMLHTTP")    }
   catch(e2) { try { minRequest = new ActiveXObject("Microsoft.XMLHTTP") }
   catch(e3) { minRequest = false }}}
   if (minRequest) minRequest.onreadystatechange = function()
   {
      if (this.readyState == 4 && this.status == 200 &&
          this.responseText != null)
            minFunk.call(this.responseText)
   }

   return minRequest
}       

getHint.php

$result = mysql_query("SELECT hint01,hint02 FROM main WHERE id = '00000001'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

你应该用

json_encode($your_array);

并用eval在javascript中解码

your_js_array = eval('(' + yourJSONtext + ')');

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM