Angular ajax POST请求无效 - 给出404错误

时间:2017-04-15 07:17:35

标签: java angularjs ajax spring-mvc

请帮忙。我无法在服务器端控制器上受到任何打击。每次我看到“404-Not Found”错误时在浏览器上。我想将post数据传递到服务器端,并希望从服务器返回任何其他字符串。以下是我的代码片段: -

web.xml servlet映射: -

<servlet-mapping>
    <servlet-name>sdnext</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

MyController.java: -

import....;

@Controller

public class MyController {

  @RequestMapping(value = "/registration", method = RequestMethod.POST)
  @ResponseBody
  public String methodStart(@RequestParam (value="UID",required=true) String json){
      System.out.println("Data recieved is:-"+json);
      return "hello";
    }
  }

客户端代码: -

var params=({
            'UID': 'testdata'
           });
$http({
            method: 'POST',
            url: 'http://192.168.1.103:8081/ProjectName/registration.html',
            headers: { 'Content-Type': 'application/json' },
            data:JSON.stringfy(params)
        }).success(function(data){
                console.log("Success Call");
            })

            .error(function(data, status) {
                console.log("Error Block");
            })

1 个答案:

答案 0 :(得分:0)

试试这个:

  1. value = "/registration"更改为,例如value = "/user-registration"以避免与其他链接冲突
  2. http://192.168.1.103:8081/ProjectName/registration.html更改为http://192.168.1.103:8081/ProjectName/registration-user,可能会导致.html后缀混淆Spring。
  3. 它可以使用,在Spring中将registration-user更改回registration,将$http网址更改为http://192.168.1.103:8081/ProjectName/registration(不带.html后缀)。我不认为您可以用这种方式在web.xml中映射AJAX请求
相关问题