wordpress全站关闭评论,找外包公司做网站价钱,怎么建免费网站,做网站搞什么流量前些天发现了一个巨牛的人工智能学习网站#xff0c;通俗易懂#xff0c;风趣幽默#xff0c;忍不住分享一下给大家。点击跳转到教程。
ajaxspringboot解决跨域问题#xff0c;以下报的错误就是html跨域的问题
Access to XMLHttpRequest at http://localhost:8080/user/l…前些天发现了一个巨牛的人工智能学习网站通俗易懂风趣幽默忍不住分享一下给大家。点击跳转到教程。
ajaxspringboot解决跨域问题以下报的错误就是html跨域的问题
Access to XMLHttpRequest at http://localhost:8080/user/login1 from origin http://localhost:59033 has been blocked by CORS policy: No Access-Control-Allow-Origin header is present on the requested resource.
springboot解决跨域的问题的两种方法
前端测试代码
!DOCTYPE html
htmlheadmeta charsetUTF-8title/title
/headbody
div idform-divform idform1p用户名input nameemail typetext idtxtUserName tabindex1 size15 value //pp密 码input namepassword typetext idTextBox2 tabindex2 size16 value //ppinput typebutton value登录 onclicklogin()nbsp;input typereset value重置/p/form
/div
/body
script typetext/javascript src../static/jquery/jquery-3.3.1.js/script
script typetext/javascriptfunction login() {$.ajax({//几个参数需要注意一下type: POST, //方法类型dataType: json, //预期服务器返回的数据类型url: http://localhost:8080/user/login1, //urldata: $(#form1).serialize(),success: function(result) {console.log(result); //打印服务端返回的数据(调试用)if(200 result.resultCode) {alert(SUCCESS);};},error: function() {alert(异常);}});}
/script/html 第一种写一个class配置的class
package com.example.demo;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;/*** Author:Yangjingcheng* Date:2018/*/
Configuration
public class CorsConfig {private CorsConfiguration buildConfig() {CorsConfiguration corsConfiguration new CorsConfiguration();corsConfiguration.addAllowedOrigin(*);corsConfiguration.addAllowedHeader(*);corsConfiguration.addAllowedMethod(*);return corsConfiguration;}Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source new UrlBasedCorsConfigurationSource();// 配置所有请求source.registerCorsConfiguration(/**, buildConfig());return new CorsFilter(source);}
}
第二种在你要访问的Controller的方法上面加上注解 CrossOrigin
package com.example.demo;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;Controller
public class TestController {CrossOriginRequestMapping(/user/login1)ResponseBodypublic ListUser userLogin(User user) {System.out.println(user);ArrayListUser users new ArrayList();for (int i 0; i 3; i) {users.add(user);}return users;}
}
OK了 转自https://blog.csdn.net/lovePaul77/article/details/85681404