Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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
Java 使用ajax post方法的Restful服务正在工作,但当我从angular调用它时,它就不工作了_Java_Angularjs Directive_Restful Architecture - Fatal编程技术网

Java 使用ajax post方法的Restful服务正在工作,但当我从angular调用它时,它就不工作了

Java 使用ajax post方法的Restful服务正在工作,但当我从angular调用它时,它就不工作了,java,angularjs-directive,restful-architecture,Java,Angularjs Directive,Restful Architecture,我用java创建了rest服务 @Path("/CommonDemo/{user}") public class CommonDemo { @POST @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.TEXT_PLAIN) public String result(@PathParam("user") String user)throws ServletException, IOException {

我用java创建了rest服务

  @Path("/CommonDemo/{user}")
  public class CommonDemo  
  {

  @POST
  @Produces(MediaType.TEXT_PLAIN)
  @Consumes(MediaType.TEXT_PLAIN)
  public String result(@PathParam("user") String user)throws ServletException, IOException 
  {
     return user;
  } 
  }
我使用ajax在html文件中创建了客户端,这将很好地工作

     function post1() 
    {
   var user="hi how are u";
   var url = "http://localhost:8080/Snefocare/CommonDemo/" +user;
       var xmlhttp = null;  
        if (window.XMLHttpRequest) {  
             xmlhttp = new XMLHttpRequest();  
               }  
         else if (window.ActiveXObject) {  
              xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  
          }  
          else {  
              alert('Whoops! Your browser does not support XMLHttpRequest!');  
          }  
           xmlhttp.onreadystatechange = function() {  
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {  
                  alert("Success");
                    alert(xmlhttp.responseText);

           }  

     };  
       xmlhttp.open('POST', url, true);  
       xmlhttp.send(null);  

   }
上面的代码运行正常,返回的结果正确,但与我需要在angular中创建客户机的结果相同,因此按照以下方式创建,但不起作用

    function post2()
   {

var user='hi how are u';

$http({method:'POST', url:'/CommonDemo', headers: {'Content-Type': 'application/x-www-form-urlencoded'},data:$.param(user)}).
  success(function(data, status, headers, config) {
    alert("success");
  }).
  error(function(data, status, headers, config) {
      alert("error");

  });

    }

此外,我还包括angularjs文件,但它不影响任何内容。

您是否验证了在使用angularjs时,请求URL和参数是什么?实际上,我是angularjs的新手,所以我不知道如何检查。