本人前端小白一个,遇到了跨域访问的问题,特来求教.如下为postman自动生成的jquery脚本,在postman中可用,但是放到某个html中后,访问时,会出现跨域访问拒绝的问题,如下图1所示。
var form = new FormData();
form.append("data", "[{\"serviceUrl\" : \"/bank_auth\",\"apiKey\" : \"3FE8D08DB99D326D\",\"areacode\":\"000000\"} ]");
var settings = {
"async": true,
"crossDomain": true,
"url": "http://xxx.xxx.com:58080/gateway-web-1.8.0/getTicket",
"method": "POST",
"headers": {
"cache-control": "no-cache",
"postman-token": "932148f8-c97b-631c-5a3e-8411a622abd9"
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});
图1
如下是我从网上找的利用jsonp的访问代码,倒是可以成功访问到getTicket接口,但是
怎么把如上面代码所示的data传给服务端呢?,求教
这问题,对于但凡有点前端基础的人,应该是很简单,但是我这种小白,是在不会呀!
<script>
function submitForm(){
var form = new FormData();
form.append(
"data",
"[{\"serviceUrl\" : \"/bank_auth\",\"apiKey\" : \"3FE8D08DB99D326D\",\"areacode\":\"000000\"} ]");
var url = 'http://xxx.xxxxx.com:58080/gateway-web-1.8.0/getTicket';
$.ajax(url, {
data: form,
dataType: 'jsonp',
crossDomain: true,
success: function(data) {
if(data && data.resultcode == '200'){
console.log(data.result.today);
}
}//success: function(data)
}); //$.ajax(url, {
}//function submitForm()
function clearForm(){
$('#ff').form('clear');
}
</script>
如下是我服务端的RESTFUL接口:
@RequestMapping(value = "/getTicket", method = { RequestMethod.POST, RequestMethod.GET })
public String executeTicket(HttpServletRequest request, HttpServletResponse response, String data)
黄舟2017-05-19 10:48:03
jsonp只能Get,你可以考虑使用CORS来跨域
function submitForm() {
var url = 'http://xxx.xxxxx.com:58080/gateway-web-1.8.0/getTicket';
$.ajax({
url: url,
method: 'post',
contentType:'application/json',
data: JSON.stringify({
serviceUrl: 'bank_auth',
apiKey: '3FE8D08DB99D326D',
areacode: '000000'
}),
dataType: 'json',
crossDomain: true,
success: function(data) {
if (data && data.resultcode == '200') {
console.log(data.result.today);
}
} //success: function(data)
}); //$.ajax(url, {
} //function submitForm()
function clearForm() {
$('#ff').form('clear');
}