JQuery ajax与POST从CORS启用服务器获取图像/ png [英] JQuery ajax with POST to get image/png from CORS enabled server

查看:752
本文介绍了JQuery ajax与POST从CORS启用服务器获取图像/ png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在撰写一个HTML5 / Backbone / Phonegap应用程式


I'm writing an HTML5/Backbone/Phonegap app Github Repo which uses the SensorObservationService REST API by 52n (v1 API Docs). Every GET-Request works fine - but now I want to download a picture that is generated after a POST request.

But the server is responding with status 400:

statusCode":400,"hints":["Check the message which has been sent to the server. Probably it is not valid."],"reason":"Bad Request","developerMessage":"Could not read JSON...

This is my AJAX-Call:

var body = {
    "base64":true,
    "legend":false,
    "timespan":"2013-10-30T00:00:00Z/2013-10-30T23:59:59Z",
    "width":482,
    "height":568,
    "language":"en",
    "grid":false,
    "styleOptions": {
        "ts_32e1174948e46f2e46fe597eb40b3557": {
            "chartType": "line",
            "properties": {
                "color": "#b45e87",
                "lineType":"solid",
                "width":1
            }
        }
    }
};
$.support.cors = true;

this.xhr = $.ajax({
     crossDomain: true,
     type: "POST",
     url:"http://sensorweb.demo.52north.org/sensorwebclient-webapp-stable/api/v1/timeseries/getData",
     processData: false,
     dataType: "json",
     accept: "application/json",
     contentType:  "application/json; charset=utf-8",
     data: body
   }).done(function(data) {
   }).fail(function(xhr, textStatus) {
   }).always(function() {
   });

Here is my fiddle - If I try the same POST call with POSTMAN the server does as he should.

RESTClient-Screenshot

What is wrong with my call?

解决方案

It was a bug in the 52n API which is - thanks to 52n - fixed by now. Here is the solution:

$.support.cors = true;
this.xhr = $.ajax({
    crossDomain: true,
    type: "POST",
    url: "http://sensorweb.demo.52north.org/sensorwebclient-webapp-stable/api/v1/timeseries/getData",
    headers: {
        "accept": "image/png",
            "content-Type": "application/json",
    },
    data: JSON.stringify(body)
}).done(function (data) {
    $('#output').html('<img src="data:image/png;base64,' + data + '" />');
});

-> JSFiddle

这篇关于JQuery ajax与POST从CORS启用服务器获取图像/ png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆