4

我正在编写一个 HTML5/Backbone/Phonegap 应用程序Github Repo,它使用 52n ( v1 API Docs ) 的 SensorObservationService REST API。每个 GET 请求都可以正常工作 - 但现在我想下载一张在 POST 请求后生成的图片。

但服务器响应状态为 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...

这是我的 AJAX 调用:

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() {
   });

这是我的小提琴- 如果我尝试使用 POSTMAN 进行相同的 POST 调用,服务器会按照他的方式进行操作。

RESTClient-截图 RESTClient 中工作调用的屏幕截图

我的电话有什么问题?

4

1 回答 1

3

这是 52n API 中的一个错误 - 感谢 52n - 现在已修复。这是解决方案:

$.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

于 2013-11-06T12:38:10.787 回答