搜尋

首頁  >  問答  >  主體

javascript - Jsonp跨域存取不會傳輸資料 求教

本人前端小白一個,遇到了跨域訪問的問題,特來求教.如下為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)
漂亮男人漂亮男人2748 天前608

全部回覆(1)我來回復

  • 黄舟

    黄舟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');
     }
    

    回覆
    0
  • 取消回覆