在 ember 组件中处理 jsonp 回调
handling a jsonp callback in an ember component
我有一个 jsonp 请求通过地理服务器检索特征信息,调用看起来像这样:
import Ember from 'ember';
export default Ember.Component.extend({
_selectParcel: function() {
function handleJson(data){
console.log(data);
}
$.ajax('url/geoserver/wms', {
type: 'GET',
data: {
service: 'WFS',
version: '1.1.0',
request: 'GetFeature',
typeName: 'name_here',
maxFeatures: 10000,
outputFormat: 'text/javascript',
srsname: 'EPSG:4326',
bbox: '-73.68229866027832, 40.97056664236637, -73.68229866027832, 40.97056664236637, EPSG:4326'
},
dataType: 'jsonp',
jsonpCallback: 'callback:handleJson',
jsonp: 'format_options'
});
}
});
我 运行 遇到的问题是处理回调范围 - 在这种情况下,handleJson()
我也试过了
.then(function(){});
在 ajax 调用之后,但没有成功。
_selectParcel 将根据鼠标移动频繁调用。
应该如何在 Ember 组件中处理 jsonp 回调?
我看过这个 using ember data with jsonp 但我不确定如何与组件中的适配器交互。
控制台错误看起来像:"Uncaught ReferenceError: handleJson is not defined" 上面写的方式 - 和 "Uncaught ReferenceError: parseResponse is not defined" 使用 callback=?和一个“.then(function(){})”承诺
尝试更改此行:
jsonpCallback: 'callback:handleJson'
到
jsonpCallback: 'handleJson'
好的,所以这里真的有2件。
- 应该ember组件请求数据
- 如何处理地理服务器 jsonp 请求
对于第一个,我发现这篇文章很有帮助Should Components Load Data
对于第二个,format_options 和 jsonpCallback 键匹配的这一点起到了作用。感谢 this link
$.ajax('url/geoserver/wms', {
type: 'GET',
data: {
service: 'WFS',
version: '1.1.0',
request: 'GetFeature',
typeName: 'name_here',
maxFeatures: 10000,
outputFormat: 'text/javascript',
srsname: 'EPSG:4326',
bbox: '-73.68229866027832, 40.97056664236637, -73.68229866027832, 40.97056664236637, EPSG:4326',
format_options: 'callback:getJson'
},
dataType: 'jsonp',
jsonpCallback: 'getJson'
}).then(function(data) {
console.log(data);
});
我有一个 jsonp 请求通过地理服务器检索特征信息,调用看起来像这样:
import Ember from 'ember';
export default Ember.Component.extend({
_selectParcel: function() {
function handleJson(data){
console.log(data);
}
$.ajax('url/geoserver/wms', {
type: 'GET',
data: {
service: 'WFS',
version: '1.1.0',
request: 'GetFeature',
typeName: 'name_here',
maxFeatures: 10000,
outputFormat: 'text/javascript',
srsname: 'EPSG:4326',
bbox: '-73.68229866027832, 40.97056664236637, -73.68229866027832, 40.97056664236637, EPSG:4326'
},
dataType: 'jsonp',
jsonpCallback: 'callback:handleJson',
jsonp: 'format_options'
});
}
});
我 运行 遇到的问题是处理回调范围 - 在这种情况下,handleJson()
我也试过了
.then(function(){});
在 ajax 调用之后,但没有成功。
_selectParcel 将根据鼠标移动频繁调用。
应该如何在 Ember 组件中处理 jsonp 回调?
我看过这个 using ember data with jsonp 但我不确定如何与组件中的适配器交互。
控制台错误看起来像:"Uncaught ReferenceError: handleJson is not defined" 上面写的方式 - 和 "Uncaught ReferenceError: parseResponse is not defined" 使用 callback=?和一个“.then(function(){})”承诺
尝试更改此行:
jsonpCallback: 'callback:handleJson'
到
jsonpCallback: 'handleJson'
好的,所以这里真的有2件。
- 应该ember组件请求数据
- 如何处理地理服务器 jsonp 请求
对于第一个,我发现这篇文章很有帮助Should Components Load Data
对于第二个,format_options 和 jsonpCallback 键匹配的这一点起到了作用。感谢 this link
$.ajax('url/geoserver/wms', {
type: 'GET',
data: {
service: 'WFS',
version: '1.1.0',
request: 'GetFeature',
typeName: 'name_here',
maxFeatures: 10000,
outputFormat: 'text/javascript',
srsname: 'EPSG:4326',
bbox: '-73.68229866027832, 40.97056664236637, -73.68229866027832, 40.97056664236637, EPSG:4326',
format_options: 'callback:getJson'
},
dataType: 'jsonp',
jsonpCallback: 'getJson'
}).then(function(data) {
console.log(data);
});