vue2.0 jsonp 與java後臺例項備忘
配置callback返回的函式名,在後臺中獲取對應函式名,否則會報錯。
import {getDataFunc} from 'api/category' import {ERR_OK} from 'api/config' export default { created(){ this._getCategory() }, data(){ return { categoryList:[] } }, methods:{ _getCategory(){ getDataFunc().then((res) => { console.log(res) // if (res.code === ERR_OK) { this.categoryList = (res.result.data) // } }) }, selectItem(item) { this.2.
import jsonp from ‘common/js/jsonp’
import {commonParams, options} from ‘api/config’
// 分類請求資料
export function getDataFunc(){
const url = ‘http://127.0.0.1:8081/qdriveradio/get_all_categories1’
const data = Object.assign({}, commonParams, {
product_id:201820182018//引數
})
console.log(jsonp(url, data,options))
return jsonp(url, data, options)
}
3.
import originJsonp from ‘jsonp’
// url應該為純淨的地址,data是資料
export default function jsonp(url, data, option) {
url += (url.indexOf(‘?’) < 0 ? ‘?’ : ‘&’) + param(data)
console.log(url)
return new Promise((resolve, reject) => {
// debugger
originJsonp(url, option, (err, data) => {
if (!err) {
resolve(data)
} else {
reject(err)
}
}
)
return new Promise(() => {})
})
}
export function param(data) {
let url = ”
for (var k in data) {
let value = data[k] !== undefined ? data[k] : ”
// url += ‘&’ + k + ‘=’ + encodeURIComponent(value)
url += &${k}=${encodeURIComponent(value)}
}
return url ? url.substring(1) : ”
}
4.
import jsonp from ‘../common/js/jsonp.js’
‘use strict’;
// 請求中的通用引數
export const commonParams = {
g_tk: 1928093487,
inCharset: ‘utf-8’,
outCharset: ‘utf-8’,
notice: 0,
format: ‘jsonp’
}
export const options = {
param: ‘jsonpCallback’,
prefix: ‘jp’//callback中的回撥function名稱
}
// 請求正確的引數碼
export const ERR_OK = 0
java:
@RequestMapping(value = “/get_all_categories1”, method = RequestMethod.GET, produces = “application/jsonp; charset=utf-8”)
public void getAllcategories1(@RequestParam Map