Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Spring控制器在Ajax get请求后不返回html页面_Javascript_Ajax_Spring Mvc - Fatal编程技术网

Javascript Spring控制器在Ajax get请求后不返回html页面

Javascript Spring控制器在Ajax get请求后不返回html页面,javascript,ajax,spring-mvc,Javascript,Ajax,Spring Mvc,因此,我有一个“折扣”html页面,其中提示用户输入促销代码,通过使用来自按钮OnClick的Ajax GET请求,我能够将此促销代码传输到我的spring控制器,在那里我可以适当地操作数据 由于某些原因,我无法从此控制器“返回”新页面,我在服务器端没有收到任何明显错误,但在客户端收到以下错误: 我不确定这是相关的还是相关的 我想知道这背后的逻辑是否有缺陷,或者我没有实现正确的语法来在AJAX调用后返回新页面 注意:AJAX请求工作正常,因为我能够将system.out.print与相关信息一起

因此,我有一个“折扣”html页面,其中提示用户输入促销代码,通过使用来自按钮OnClick的Ajax GET请求,我能够将此促销代码传输到我的spring控制器,在那里我可以适当地操作数据

由于某些原因,我无法从此控制器“返回”新页面,我在服务器端没有收到任何明显错误,但在客户端收到以下错误:

我不确定这是相关的还是相关的

我想知道这背后的逻辑是否有缺陷,或者我没有实现正确的语法来在AJAX调用后返回新页面

注意:AJAX请求工作正常,因为我能够将system.out.print与相关信息一起发送到控制器底部的控制台。我通过了

以下是我的html代码:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
 <meta name="viewport" content="width=device-width, initial-scale=1"></meta>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></link>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript">
function myFunction(){


        var code = document.getElementById("code").value;
         var price = document.getElementById("price").value;

    $.ajax({
  type: "GET",
  url: "/calculateDiscount",
  data: {  
          code: code
    }, // parameters

    contentType: "application/json; charset=utf-8",
datatype: 'json'
//alert(status);
});
}

</script>

</head>
<body>

<div>
<center>

    <h3>Total Price: $<text th:text="${totalPrice}" id="price"/> </h3>
    <input type="text" name="firstname" id="code">
   <button onclick="myFunction()">Calculate Discount</button> 


       <a style="color:blue" th:href="@{/welcome}">HomeScreen!</a>
       <br />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

<!--       <a style="color:blue" th:if="${myteam != null}" th:href="@{/leaveteam/{id}(id=${myteam?.id})}">Leave Team?!</a>
 -->
</center>       
</div>



</body>
</html>
更新:@ResponseBody注释已添加到控制器方法,问题仍然存在

我想这就是问题所在。您已经实现了Spring安全性(我可以看到
securitycontextheholder
class),可能已启用csrf-这是默认设置。如果你想禁用它,就用这个

Java配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
}
XML:


500
表示存在服务器端异常,它应该有一些日志,您是否配置了
log4j
?@lucumt我刚刚在那里更新了代码,我没有收到任何500错误。那是我以前犯的一个错误。但是不,我没有实现log4jyou ajax类型是
json
,控制器返回类型是string,您需要保持它们与ajax相同的移动数据类型和内容类型,并在提交ajax调用时进行尝试,spring方法需要使用@ResponseBody注释进行注释。我已使用您所指的websecurityclass更新了我的问题,该类已禁用csrf。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
 <meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<title>Insert title here</title>
</head>
<body>
    <h3>Total Price: $<text th:text="${totalPrice}" id="price"/> </h3>
</body>
</html>
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
       UserLoginRepository userLoginRepository;

    //http.authorizeRequests().antMatchers("/", "/home", "/registeruser").permitAll().antMatchers("/admin").hasRole("ADMIN")

     @Autowired
     DataSource dataSource;

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().antMatchers("/", "/home", "/registeruser").permitAll().antMatchers("/admin").hasRole("ADMIN")
                    .anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout()
                    .permitAll();
            http.exceptionHandling().accessDeniedPage("/403");
            http.csrf().disable();
            //disable csrf to allow communication (we also dont need for this fyp as its not live)
        }

        @Override
        public void configure(WebSecurity web) {
            web.ignoring().antMatchers("/fonts/**", "/images/**", "/css/**");
        }



    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {


           auth.jdbcAuthentication().dataSource(dataSource)
          .usersByUsernameQuery("select user_name,password,user_status from user_login where user_name=?")
         .authoritiesByUsernameQuery("select user_name, password from user_login where user_name=?");          


}

    @Bean
    public static NoOpPasswordEncoder passwordEncoder() {
     return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }

    }
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
}
<http>
    <!-- ... -->
    <csrf disabled="true"/>
</http>
<head>
    <meta name="_csrf" th:content="${_csrf.token}"/>
    <meta name="_csrf_header" th:content="${_csrf.headerName}"/>
var token = /*[[${_csrf.token}]]*/ '';
var header = /*[[${_csrf.headerName}]]*/ '';

$(document).ajaxSend(function(e,xhr,options) {
        xhr.setRequestHeader(header, token);
});