2017-06-06 83 views
0

我想運行POST AJAX,但發現錯誤>預檢的響應具有無效的HTTP狀態代碼403.我在Spring中創建了POST方法,並且希望使用AJAX我使用基於javascript的SAPUI5前端)。AJAX錯誤預檢的響應中包含無效的HTTP狀態代碼403至Spring

Error and Invalid CORS Request

Headers

登錄時運行彈簧(Maven項目):

org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$793b4734] 
> is not eligible for getting processed by all BeanPostProcessors (for 
> example: not eligible for auto-proxying) [INFO] Loaded JDBC driver: 
> com.mysql.jdbc.Driver [INFO] HCANN000001: Hibernate Commons 
> Annotations {4.0.5.Final} [INFO] HHH000412: Hibernate Core 
> {4.3.11.Final} [INFO] HHH000206: hibernate.properties not found [INFO] 
> HHH000021: Bytecode provider name : javassist [INFO] HHH000400: Using 
> dialect: org.hibernate.dialect.MySQLDialect [INFO] HHH000423: 
> Disabling contextual LOB creation as JDBC driver reported JDBC version 
> 2 less than 4 [INFO] HHH000399: Using default transaction strategy 
> (direct JDBC transactions) [INFO] HHH000397: Using 
> ASTQueryTranslatorFactory [INFO] Mapped 
> "{[/tesupdate],methods=[PUT],consumes=[application/json]}" onto public 
> org.springframework.http.ResponseEntity<?> 
> com.project.maven.controller.UserController.update(com.project.maven.entity.KasusEntity) 
> throws java.lang.Exception [INFO] Mapped 
> "{[/tesinsert],methods=[POST],consumes=[application/json]}" onto 
> public org.springframework.http.ResponseEntity<?> 
> com.project.maven.controller.UserController.insert(com.project.maven.entity.KasusEntity) 
> throws java.lang.Exception [INFO] Mapped "{[/**],methods=[OPTIONS]}" 
> onto public void 
> com.project.maven.controller.UserController.corsHeaders(javax.servlet.http.HttpServletResponse) 
> [INFO] Mapped "{[/tesget],methods=[GET]}" onto public java.util.List 
> com.project.maven.controller.UserController.getTes2() throws 
> java.lang.Exception [INFO] Mapped 
> "{[/v2/api-docs],methods=[GET],produces=[application/json || 
> application/hal+json]}" onto public 
> org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> 
> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest) 
> [INFO] Mapped "{[/swagger-resources/configuration/security]}" onto 
> org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> 
> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration() 
> [INFO] Mapped "{[/swagger-resources/configuration/ui]}" onto 
> org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> 
> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration() 
> [INFO] Mapped "{[/swagger-resources]}" onto 
> org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> 
> springfox.documentation.swagger.web.ApiResourceController.swaggerResources() 
> [INFO] Mapped URL path [/static/**] onto handler of type [class 
> org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
> [INFO] Mapped URL path [/**/**] onto handler of type [class 
> org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
> [INFO] Looking for @ControllerAdvice: Root WebApplicationContext: 
> startup date [Tue Jun 06 10:22:39 ICT 2017]; root of context hierarchy 
> [INFO] Using DataSource 
> [[email protected]a047] 
> of Hibernate SessionFactory for HibernateTransactionManager [INFO] 
> Starting beans in phase 2147483647 [INFO] Context refreshed [INFO] 
> Found 1 custom documentation plugin(s) [INFO] Scanning for api listing 
> references [INFO] Root WebApplicationContext: initialization completed 
> in 7923 ms Jun 06, 2017 10:22:47 AM 
> org.apache.catalina.core.ApplicationContext log INFO: Initializing 
> Spring FrameworkServlet 'appServlet' [INFO] FrameworkServlet 
> 'appServlet': initialization started [INFO] Refreshing 
> WebApplicationContext for namespace 'appServlet-servlet': startup date 
> [Tue Jun 06 10:22:47 ICT 2017]; parent: Root WebApplicationContext 
> [INFO] Loading XML bean definitions from ServletContext resource 
> [/WEB-INF/config/servlet-context.xml] [INFO] FrameworkServlet 
> 'appServlet': initialization completed in 50 ms Jun 06, 2017 10:22:47 
> AM org.apache.catalina.core.ApplicationContext log INFO: Initializing 
> Spring FrameworkServlet 'dispatcher' [INFO] FrameworkServlet 
> 'dispatcher': initialization started [INFO] Refreshing 
> WebApplicationContext for namespace 'dispatcher-servlet': startup date 
> [Tue Jun 06 10:22:47 ICT 2017]; parent: Root WebApplicationContext 
> [INFO] Registering annotated classes: [class 
> com.project.maven.config.AppConfig] [INFO] JSR-330 
> 'javax.inject.Inject' annotation found and supported for autowiring 
> [INFO] Bean 
> 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' 
> of type [class 
> org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$793b4734] 
> is not eligible for getting processed by all BeanPostProcessors (for 
> example: not eligible for auto-proxying) [INFO] Loaded JDBC driver: 
> com.mysql.jdbc.Driver [INFO] HHH000400: Using dialect: 
> org.hibernate.dialect.MySQLDialect [INFO] HHH000423: Disabling 
> contextual LOB creation as JDBC driver reported JDBC version 2 less 
> than 4 [INFO] HHH000399: Using default transaction strategy (direct 
> JDBC transactions) [INFO] HHH000397: Using ASTQueryTranslatorFactory 
> [INFO] Mapped 
> "{[/tesupdate],methods=[PUT],consumes=[application/json]}" onto public 
> org.springframework.http.ResponseEntity<?> 
> com.project.maven.controller.UserController.update(com.project.maven.entity.KasusEntity) 
> throws java.lang.Exception [INFO] Mapped 
> "{[/tesinsert],methods=[POST],consumes=[application/json]}" onto 
> public org.springframework.http.ResponseEntity<?> 
> com.project.maven.controller.UserController.insert(com.project.maven.entity.KasusEntity) 
> throws java.lang.Exception [INFO] Mapped "{[/**],methods=[OPTIONS]}" 
> onto public void 
> com.project.maven.controller.UserController.corsHeaders(javax.servlet.http.HttpServletResponse) 
> [INFO] Mapped "{[/tesget],methods=[GET]}" onto public java.util.List 
> com.project.maven.controller.UserController.getTes2() throws 
> java.lang.Exception [INFO] Mapped 
> "{[/v2/api-docs],methods=[GET],produces=[application/json || 
> application/hal+json]}" onto public 
> org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> 
> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest) 
> [INFO] Mapped "{[/swagger-resources/configuration/security]}" onto 
> org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> 
> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration() 
> [INFO] Mapped "{[/swagger-resources/configuration/ui]}" onto 
> org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> 
> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration() 
> [INFO] Mapped "{[/swagger-resources]}" onto 
> org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> 
> springfox.documentation.swagger.web.ApiResourceController.swaggerResources() 
> [INFO] Using DataSource 
> [[email protected]8a87] 
> of Hibernate SessionFactory for HibernateTransactionManager [INFO] 
> Mapped URL path [/static/**] onto handler of type [class 
> org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
> [INFO] Mapped URL path [/**/**] onto handler of type [class 
> org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
> [INFO] Looking for @ControllerAdvice: WebApplicationContext for 
> namespace 'dispatcher-servlet': startup date [Tue Jun 06 10:22:47 ICT 
> 2017]; parent: Root WebApplicationContext [INFO] Starting beans in 
> phase 2147483647 [INFO] Context refreshed [INFO] Found 1 custom 
> documentation plugin(s) [INFO] Scanning for api listing references 
> [INFO] FrameworkServlet 'dispatcher': initialization completed in 1351 
> ms Jun 06, 2017 10:22:48 AM org.apache.coyote.AbstractProtocol start 
> INFO: Starting ProtocolHandler ["http-bio-8089"] 

控制器春:

@PostMapping(value={"/tesinsert"}, consumes={"application/json"}) 
@ResponseStatus(HttpStatus.CREATED) 
public ResponseEntity<?> insert(@RequestBody KasusEntity user) throws Exception { 
    Map result = new HashMap(); 
    userService.insertTabel(user); 
    return new ResponseEntity<>(result, HttpStatus.CREATED); 
} 

在javascript:

var data = { 
      "kodekasus":5, 
      "nama":"baru", 
      "isdelete":1, 
      "createdby":"hahaa", 
      "createddate":null, 
      "updatedby":"hihii", 
      "updateddate":null 
    }; 

    var url = "http://localhost:8089/SpringNew/tesinsert"; 

    $.ajax({ 
     type: 'POST', 
     url: url, 
     dataType: 'json', 
     data: JSON.stringify(data), 
     success: function(data) { 
      console.log('sukses: '+data); 
     }, 
     error: function(error){ 
      console.log('gagal: '+error); 
      console.log(data); 
     } 
     , 
     headers: { 
      Accept : "application/json", 
      "Content-type": "application/json;charset=UTF-8" 
      } 
    }); 

如何解決這個問題?代碼錯了?在JavaScript或春天?我已經嘗試使用Swagger在Spring中運行插入方法,並且我可以插入數據。我已經嘗試ajax GET並沒有問題。在此之前,我還發現了AJAX GET的問題,但我用ajax中清晰的「contentType」解決了這個問題。但在AJAX POST中不能。

謝謝。

鮑比

+0

在你的ajax調用中,不需要.stringify(data),只需要data:data, –

+0

我已經試過沒有stringify但是顯示相同的錯誤。 – Bobby

+0

你能否展示你的實體課(KasusEntity) –

回答

0

這是我的代碼KasusEntity:

package com.project.maven.entity; 

import java.io.Serializable; 
import java.util.Date; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name="KASUS") 
public class KasusEntity implements Serializable { 

    @Id 
    @Column(name="KODE_KASUS") 
    private int kodekasus; 

    @Column(name="NAMA") 
    private String nama; 

    @Column(name="IS_DELETE") 
    private int isdelete; 

    @Column(name="CREATED_BY") 
    private String createdby; 

    @Column(name="CREATED_DATE") 
    private Date createddate;  

    @Column(name="UPDATED_BY") 
    private String updatedby; 

    @Column(name="UPDATED_DATE") 
    private Date updateddate; 

    public int getKodekasus() { 
     return kodekasus; 
    } 

    public void setKodekasus(int kodekasus) { 
     this.kodekasus = kodekasus; 
    } 

    public String getNama() { 
     return nama; 
    } 

    public void setNama(String nama) { 
     this.nama = nama; 
    } 

    public int getIsdelete() { 
     return isdelete; 
    } 

    public void setIsdelete(int isdelete) { 
     this.isdelete = isdelete; 
    } 

    public String getCreatedby() { 
     return createdby; 
    } 

    public void setCreatedby(String createdby) { 
     this.createdby = createdby; 
    } 

    public Date getCreateddate() { 
     return createddate; 
    } 

    public void setCreateddate(Date createddate) { 
     this.createddate = createddate; 
    } 

    public String getUpdatedby() { 
     return updatedby; 
    } 

    public void setUpdatedby(String updatedby) { 
     this.updatedby = updatedby; 
    } 

    public Date getUpdateddate() { 
     return updateddate; 
    } 

    public void setUpdateddate(Date updateddate) { 
     this.updateddate = updateddate; 
    } 

} 

感謝。 Bobby

相關問題