js ajax 前端跨域问题最简单的解法

  • Post author:
  • Post category:其他


采用nginx转发端口就行了,跨域请求后台时,只要在接口前加 api这个子母(即下面代码中的api,可自行更改),不影响后台接口,不用改后台接口。例如后台接口为 /robot/getAll,跨域请求时,只需要这样写:/api/robot/getAll。一次设置,永久有效。

前端代码:假设nginx服务器ip为123.123.123.123,端口为12004

$.ajax({type:”GET”,

async:true,

dataType: ‘json’,

url:”http://123.123.123.123:12004/api/robot/another…”, //这里的api是必须的,服务器IP为123.123.123.123

crossDomain: true,

processData: false,//用于对data参数进行序列化处理 这里必须false

contentType: false, //必须

complete: function(result, a,b,c,d){

console.log(‘complete result’, result, a,b);

},

success: function(result){

console.log(‘success result’, result);

},

error:function(result, a,b){

console.log(‘error re



版权声明:本文为fdsjiaoDoctor原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。