2013-10-30 59 views
3

我正在編寫一個HTML5/Backbone/Phonegap應用程序Github Repo,它使用52n(v1 API Docs)的SensorObservationService REST API。 每個GET-Request都可以正常工作 - 但現在我想要下載POST請求後生成的圖片。JQuery ajax與POST從CORS啓用服務器獲取圖像/ png

但是服務器與狀態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() { 
    }); 

這裏是我的fiddle - 如果我嘗試同樣的POST通過POSTMAN與服務器通話。

RESTClient實現-截圖 Screenshot with working Call in RESTClient

什麼是錯我的電話?

回答

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