http://www.123.com/index.html 调用 http://www.123.com/server.php (非跨域)
http://www.123.com/index.html 调用 http://www.456.com/server.php (主域名不同:123/456,跨域)
http://abc.123.com/index.html 调用 http://def.123.com/server.php (子域名不同:abc/def,跨域)
http://www.123.com:8080/index.html 调用 http://www.123.com:8081/server.php (端口不同:8080/8081,跨域)
http://www.123.com/index.html 调用 https://www.123.com/server.php (协议不同:http/https,跨域)
请注意:localhost和127.0.0.1虽然都指向本机,但也属于跨域。
For security reasons, browsers prohibit AJAX calls to resources residing outside the current origin. For example, as you’re checking your bank account in one tab, you could have the evil.com website open in another tab. The scripts from evil.com should not be able to make AJAX requests to your bank API (e.g., withdrawing money from your account!) using your credentials.
出于安全原因,浏览器禁止对当前来源之外的资源进行AJAX调用。 例如,当您在一个选项卡中检查您的银行帐户时,您可以在另一个选项卡中打开evil.com网站。 来自evil.com的脚本不应该使用您的凭据向您的银行API发出AJAX请求(例如,从您的帐户中提取资金!)。
package com.zhang.web;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class JsonpController {
/**
* 跨域使用
* <p>Title: index</p>
* <p>Description: </p>
* @return
*/
@RequestMapping(value="testjsonp/postjsonpdata", method = RequestMethod.POST)
@ResponseBody
public Object postJsonpData(String callback) {
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue("postjsonpdata");
//设置回调方法
mappingJacksonValue.setJsonpFunction(callback);
return mappingJacksonValue;
}
/**
* 跨域使用
* <p>Title: index</p>
* <p>Description: </p>
* @return
*/
@RequestMapping(value="testjsonp/getjsonpdata", method = RequestMethod.GET)
@ResponseBody
public Object getJsonpData(String callback) {
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue("getjsonpdata");
//设置回调方法
mappingJacksonValue.setJsonpFunction(callback);
return mappingJacksonValue;
}
/**
* 跨域使用
* <p>Title: index</p>
* <p>Description: 自己拼接返回的数据</p>
* @return
*/
@RequestMapping(value="testjsonp/getjsonpdatabystitching", method = RequestMethod.GET)
@ResponseBody
public String getJsonpDataByStitching(String callback) {
String strResult = callback + "('getjsonpdataByStitching')";
return strResult;
}
/**
* 跨域使用
* <p>Title: index</p>
* <p>Description: 自己拼接返回的数据</p>
* @return
*/
@RequestMapping(value="testjsonp/postjsonpdatabystitching", method = RequestMethod.POST)
@ResponseBody
public String postJsonpDataByStitching(String callback) {
String strResult = callback + "('postjsonpdataByStitching')";
return strResult;
}
}
$.ajax({
type: 'get',
url: "http://localhost:8080/testjsonp/getjsonpdatabystitching",
dataType:"jsonp",
success: function(res){
alert(res)
}
});
package com.zhang.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class IndexController {
/**
*
* <p>Title: getdata</p>
* <p>Description: 跨域使用get方法</p>
* @return
*/
@RequestMapping(value="testcrossorigin/getdata", method = RequestMethod.GET)
@ResponseBody
public String getdata() {
return "getdata";
}
/**
*
* <p>Title: postdata</p>
* <p>Description: 跨域使用post方法</p>
* @return
*/
@RequestMapping(value="testcrossorigin/postdata", method = RequestMethod.POST)
@ResponseBody
public String postdata() {
return "postdata";
}
/**
*
* <p>Title: postdata</p>
* <p>Description: 跨域使用 并不加方法直接调用</p>
* @return
*/
@RequestMapping(value="testcrossorigin/data")
@ResponseBody
public String data() {
return "data";
}
}
$.ajax({
type: 'get',
url: "http://localhost:8080/testcrossorigin/data",
success: function(res){
alert(res)
}
});
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |