使用XMLHttpRequest从web服务获取图像

Get an image from a web service with XMLHttpRequest

本文关键字:获取 图像 服务 web XMLHttpRequest 使用      更新时间:2023-09-26

我想从web服务中获取一个图像。连接到web服务已经成功,但图像没有在我的phoneGap应用程序中打开,也没有在Mozilla中打开。。。这是我的代码:

       var request = new XMLHttpRequest();
   request.open("GET",'http://patisserie-orient.fr/prestashop/prestashop/api/images/products/10/50?PHP_AUTH_USER=motdepasse&ws_key=login',true);
            request.onreadystatechange = function()
                {
                    if(request.readyState==4)
                    {
                       if (request.status == 200 || request.status == 0)
                        {

             alert("Status2 is  "+request.status);
                document.getElementById("myimg").src='http://patisserie-orient.fr/prestashop/prestashop/api/images/products/10/50?PHP_AUTH_USER=motdepasse&ws_key=login';
                        }
                     }
                 }
    request.send();
}

您是否能够使用此URL 获取图像

http://patisserie-orient.fr/prestashop/prestashop/api/images/products/10/50?PHP_AUTH_USER=motdepasse&ws_key=登录

如果是,那么你可以做的就是在HTML中给出这个<img src="http://patisserie-orient.fr/prestashop/prestashop/api/images/products/10/50?PHP_AUTH_USER=motdepasse&ws_key=login"

如果您获得带有URL的图像,Ajax就没有必要这样做。

甚至

<img src="http://patisserie-orient.fr/prestashop/prestashop/api/images/products/10/50?PHP_AUTH_USER=motdepasse&ws_key=login" onError="this.src='productorservernotavailable.gif'" />