JQuery的阿贾克斯与POST来获得图像/从CORS启用的服务器png格式 [英] JQuery ajax with POST to get image/png from CORS enabled server
问题描述
我在写一个HTML5 /骨干网/ PhoneGap的应用程序 GitHub库它使用SensorObservationService REST API通过52N( V1 API文档) 。 每一个GET请求工作正常 - 但现在我想下载的是一个POST请求后生成的图片
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.
但服务器响应状态为400:
But the server is responding with status 400:
状态code:400,提示:检查已发送到服务器的消息。也许这是无效],理由:错误的请求,developerMessage:无法读取JSON
...
这是我的AJAX调用:
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-截图
RESTClient-Screenshot
什么是错我的电话?
推荐答案
这是52N API,这是一个错误 - 由于52N - 修复了。 这里是解决方案:
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的阿贾克斯与POST来获得图像/从CORS启用的服务器png格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!