PHP和JAVA之间的跨域请求(CORS)问题

时间:2017-05-25 15:10:31

标签: java php cors http-post cross-domain

当我尝试从我的php应用程序向我的控制器发送POST请求时,我遇到了问题。

这是我从php(客户端)的请求:

$.ajax({
    url: "http://myURL:8080/myApplication/myMethod",
    contentType: "application/json; charset=UTF-8",
    crossDomain: true,
    dataType: "json",
    method: "post",
    data: data,
    success: function (data) {
        /*things to do */
    }
});

我在html上包含档案:

<!DOCTYPE html>
<html>
    <body  onload="start()">
    <head lang="pt-br">
    <meta charset="utf-8">
    <script type="text/javascript" src="https://www.google.com/jsapi">
    </script>
    <link rel="stylesheet"          
    href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"> </script>
    <script src="./jquery/jquery.ajax-cross-origin.min.js"></script>
    ...
    ajaxRequestMethod above
    </end tags>
</html>

这是在服务器端,在我的控制器上的java:

@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(value = "/myMehtod", method = RequestMethod.POST)
public @ResponseBody List<Alimento> calculo(@RequestBody PlanoAlimentarWrapper wrapper) {
    PlanoAlimentarWrapper melhorPlano = new PlanoAlimentarWrapper();
    HashMap<Alimento, Integer> alimentos = new HashMap<>();
    wrapper.createBestPlan(melhorPlano, wrapper.getPlano(), alimentos);
    List<Alimento> retornoMelhorPlano = new ArrayList<Alimento>();
    retornoMelhorPlano.addAll(melhorPlano.getPlano());
    return retornoMelhorPlano;
}

当我尝试请求此方法时,我收到以下错误:

Object {readyState=0, status=0, statusText="error", ...}

它可能是什么?

0 个答案:

没有答案