'Access-Control-Allow-Origin' error in Spring MVC + Zepto POST

后端 未结 4 2047
终归单人心
终归单人心 2020-12-19 17:23

I\'m trying to POST a JSON object to my Spring MVC controller, but I only receive an Access-Control-Allow-Origin error.

My controller:

@         


        
4条回答
  •  旧巷少年郎
    2020-12-19 18:26

    This is because the page is being loaded from a server in http://localhost:9000, and this page is trying to access via an ajax request a different domain http://127.0.0.1:8080/AgenceVoyage/user/add.

    Notice that the different port number makes these two urls correspond to two different domains, even tough they are both localhost urls.

    Because the two are considered different domains, the browser ajax Same Origin Policy kicks in and prevents the request from being executed due to security reasons (ajax requests against third party domains are not allowed).

    See here for more details on the ajax Same Origin Policy, specially the origin determination policy section where it's mentioned that the port number is part of the origin.

提交回复
热议问题