doulou1989 2019-06-09 04:55
浏览 98

我可以在javascript中使用JSON来获取服务器上的php数组文件以显示数据吗?

I am trying to get a .php file on a server in my neatbeans software using JavaScript with JSON. I am a begginer and I am not finding any solution.

I get this error in developers tool:

Access to XMLHttpRequest at 'http://evercodeplus.com/get_products.php' from origin 'http://localhost:8084' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Also I don't know why the pro.insertAdjacentHTML is not working.

I have seen people mentioning json_encode, but I don't know how to use it.

        xmlhttp.open('GET', 'http://evercodeplus.com/get_products.php'  , true);
        xmlhttp.onload = function (){
        var ourData = JSON.parse(xmlhttp.responseText);
        showHTML(ourData);
    };
    xmlhttp.send();
};

function showHTML(data)
{
    var htmlString = "";
    for(i=0; i<data.length; i++)
    {
        htmlString += "<ul>" + data[i].name + ".</ul>";
    }

    document.getElementById("product").innerHTML=htmlString;
    pro.insertAdjacentHTML('beforeend','htmlString');
}
  • 写回答

1条回答 默认 最新

  • dongliyu3278 2019-06-09 07:24
    关注

    If you prefer json_encode, then the function shoud wrap the return array result of get_product.php. The return result will be in json format so that you can process them.

    For CORS, make sure the server allow cross origin resource sharing and check if your request method support CORS.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料