通过 jQuery AJAX 获取图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17339876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:50:58  来源:igfitidea点击:

Get an image through jQuery AJAX

asp.net-mvc-3jqueryimagesource

提问by Paul Alwin

The following image tag is in my Login Page

以下图片标签在我的登录页面中

<img id="CaptchaImg" alt="Captcha" src="@url.action("Captcha","Login")" style="" />

Now when I refresh my login Page, it works fine, and gets the image from the controller, but when I logOut and the login Page renders .the controller method is not being called.Problem is only in IE works fine on Chrome.Is there are work around? Can I do something with jQuery Ajax call to the controller, I tried this but the success method is not called. Is there any other way?

现在,当我刷新登录页面时,它工作正常,并从控制器获取图像,但是当我注销并呈现登录页面时。没有调用控制器方法。问题仅在 IE 中在 Chrome 上工作正常。有吗解决了吗?我可以用 jQuery Ajax 调用控制器做些什么吗,我试过了,但没有调用成功方法。有没有其他办法?

$.ajax({
    type: "GET",
    url: '/Login/CaptchaImage',
    datatype: "image",
    success: function(data) {
        debugger
        $('#CaptchaImg').attr('src', data);
    }
});

回答by Amit

Try this

尝试这个

$.ajax({
    type: "GET",
    url: '@Url.Action("Captcha","Login")',
    dataType:"image/jpg",
    success: function (data) {
        $('#CaptchaImg').attr('src', data);
    }
 });

回答by Vincent

web Server should return base64 Data. like this

web 服务器应该返回 base64 数据。像这样

{ base64Data:"blahblahblah" }

your ajax request content-type could be plain json ; in your success function in ajax you could do like this

您的 ajax 请求内容类型可能是纯 json ;在 ajax 的成功函数中,你可以这样做

    success:function(data){
     var src="data:image/png;base64,"+data.base64Data;
      $('#CaptchaImg').attr(src,src)
}