[英]Javascript to jquery Ajax request
我有一些使用Ajax
向Tomcat
服务器发出请求的Javascript
。 我在Tomcat
服务器上设置了过滤器,以允许使用以下命令进行跨域请求:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这是我已成功向Tomcat
servelet发出请求的Javascript
代码段:
$(function() {
// Set the default dates
var startDate = Date.create().addDays(-6), // 7 days ago
endDate = Date.create(); // today
var range = $('#range');
// Show the dates in the range input
range.val(startDate.format('{MM}/{dd}/{yyyy}') + ' - '
+ endDate.format('{MM}/{dd}/{yyyy}'));
// Load chart
ajaxLoadChart(startDate, endDate);
range.daterangepicker({
startDate : startDate,
endDate : endDate,
ranges : {
'Today' : [ 'today', 'today' ],
'Yesterday' : [ 'yesterday', 'yesterday' ],
'Last 7 Days' : [ Date.create().addDays(-6), 'today' ],
'Last 30 Days' : [ Date.create().addDays(-29), 'today' ]
}
}, function(start, end) {
ajaxLoadChart(start, end);
});
// Function for loading data via AJAX and showing it on the chart
function ajaxLoadChart(startDate, endDate) {
// If no data is passed (the chart was cleared)
if (!startDate || !endDate) {
return;
}
// Otherwise, issue an AJAX request
$.post('http://192.168.1.6:8083/Servlet/PreProcessor', {
start : startDate.format('{MM}/{dd}/{yyyy}'),
end : endDate.format('{MM}/{dd}/{yyyy}')
}, function(data) {
if ((data.indexOf("No record found") > -1)
|| (data.indexOf("Date must be selected.") > -1)) {
$('#msg').html('<span style="color:red;">' + data + '</span>');
} else {
$('#msg').empty();
$('#chart').highcharts({
chart : {
type : 'arearange',
zoomType : 'x'
},
title : {
text : 'Temperature variation by day'
},
xAxis : {
type : 'datetime'
},
yAxis : {
title : {
text : null
}
},
tooltip : {
crosshairs : true,
shared : true,
valueSuffix : '°C'
},
legend : {
enabled : false
},
series : [ {
name : 'Temperatures',
data : data
} ]
});
}
}, 'json');
}
});
我正在尝试将Javscript
Ajax
请求转换为Jquery
,但是我No 'Access-Control-Allow-Origin' header is present on the requested resource
错误和我的Jquery Ajax请求的POST
错误中No 'Access-Control-Allow-Origin' header is present on the requested resource
Javscript
No 'Access-Control-Allow-Origin' header is present on the requested resource
:
$(function () {
// Set the default dates
var startDate = Date.create().addDays(-6), // 7 days ago
endDate = Date.create(); // today
var range = $('#range');
// Show the dates in the range input
range.val(startDate.format('{MM}/{dd}/{yyyy}') + ' - '
+ endDate.format('{MM}/{dd}/{yyyy}'));
// Load chart
ajaxLoadChart(startDate, endDate);
range.daterangepicker({
startDate: startDate,
endDate: endDate,
ranges: {
'Today': ['today', 'today'],
'Yesterday': ['yesterday', 'yesterday'],
'Last 7 Days': [Date.create().addDays(-6), 'today'],
'Last 30 Days': [Date.create().addDays(-29), 'today']
}
}, function (start, end) {
ajaxLoadChart(start, end);
});
// Function for loading data via AJAX and showing it on the chart
function ajaxLoadChart(startDate, endDate) {
// If no data is passed (the chart was cleared)
if (!startDate || !endDate) {
return;
}
// Otherwise, issue an AJAX request
$.ajax({
url: 'http://192.168.1.6:8083/Servlet/PreProcessor',
type: 'POST',
crossDomain: true,
async: true,
dataType: "json",
start: startDate.format('{MM}/{dd}/{yyyy}'),
end: endDate.format('{MM}/{dd}/{yyyy}'),
success: function (data) {
defaultChart(data);
}
});
}
});
function defaultChart(data) {
if ((data.indexOf("No record found") > -1)
|| (data.indexOf("Date must be selected.") > -1)) {
$('#msg').html('<span style="color:red;">' + data + '</span>');
} else {
$('#msg').empty();
$('#chart').highcharts({
chart: {
type: 'arearange',
zoomType: 'x'
},
title: {
text: 'Temperature variation by day'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: null
}
},
tooltip: {
crosshairs: true,
shared: true,
valueSuffix: '°C'
},
legend: {
enabled: false
},
series: [{
name: 'Temperatures',
data: data
}]
});
}
}
我是Jquery
和Javascript
的新手,看不到我出了问题,研究向我建议,将crossDomain: true
添加到Ajax Jquery请求中会在请求中设置标头,所以也许我的代码是问题?
编辑
再看看这个,已经是使用Jquery的第一个代码片段了吗?
第一个代码段是使用jQuery。 $ Post语法糖,用于将$ .ajax与type =“ POST”一起使用。 我不确定第二个代码为什么会出现CORS错误,而第一个代码却没有。 您可以使用chrome开发人员工具对标头进行故障排除,以查看请求和响应上的标头。
或者,如果要发布到相同的URL,则可以使用相对地址而不是标准的HTTP://地址。
所以我的代码的问题是您向Ajax
请求添加参数的方式
我的尝试尝试的结构如下:
$.ajax({
url: 'http://192.168.1.6:8083/Servlet/PreProcessor',
type: 'POST',
crossDomain: true,
async: true,
dataType: "json",
start: startDate.format('{MM}/{dd}/{yyyy}'),
end: endDate.format('{MM}/{dd}/{yyyy}'),
success: function (data) {
defaultChart(data);
}
});
何时应该这样构造:
$.ajax({
url: 'http://192.168.1.6:8083/IBMServlet_war/PreProcessor',
crossDomain: true,
async: true,
type: "POST",
dataType: "json",
data:{ start: startDate.format('{MM}/{dd}/{yyyy}'), end : endDate.format('{MM}/{dd}/{yyyy}')},
success: function (data) {
defaultChart(data);
}
}).error(function(xhr, status, error) {
alert("error");
console.log(xhr);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.