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.

    评论

报告相同问题?

悬赏问题

  • ¥15 SQL Server下载
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求