2016-01-06 173 views
0

我试图使用asp.net。当我尝试调用服务,我得到错误发布数据到Java REST服务响应错误:无法加载资源:服务器400(错误请求)的状态

Failed to load resource: the server responded with a status of 400 (Bad Request).

请指导我在这我GOOGLE了一下但没有发现solution.I已经启用了我的服务中加入代码CORS领域工作,因为在服务器应用下面提到

@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) 
public @ResponseBody Status addEmployee(@RequestBody Employee employee, HttpServletRequest request, HttpServletResponse response) { 
    try { 
     response.addHeader("Access-Control-Allow-Origin","*"); 
     dataServices.addEntity(employee); 

     return new Status(1, "Employee added Successfully !"); 
    } catch (Exception e) { 
     // e.printStackTrace(); 
     return new Status(0, e.toString()); 
    } 

} 

在asp.net APPLICAT离子

 $.ajax({ 
      crossDomain: true, 
      type: 'POST', 
      url: "http://localhost:9091/employeeservice/employee/create", 
      data: JSON.stringify({ 
       "id": 10, 
       "first_name": "narayan", 
       "last_name": "bhat", 
       "email": "[email protected]", 
       "phone": "4545454545" 
      }), 
      error: OnErrorCall, 
      success: OnSuccessCall, 
      dataType: "json", 
      contentType: "application/json" 
     }); 


     function OnSuccessCall(response) { 
      alert(response.d); 
     } 


     function OnErrorCall(response) { 
      alert(response.status + " " + response.statusText); 
     } 

请指导我在这里我是新来的asp.net。

回答

0

我建议您至少尝试RestSharp以发布&从Rest API获取响应。如果您想使用Rest API代码隐藏功能,此方法非常有用。

我已经尝试过你前几天调用Rest服务的方式,并从服务器得到400错误。之后,我尝试了RestSharp,它适用于我。

您可以要求&得到回应像下面使用RestSharp

var restClient = new RestClient("https://www.example.com"); 
var request = new RestRequest(Method.POST); 
request.Resource = "{version}/adduser"; 
request.AddParameter("version", "v1", ParameterType.UrlSegment); 

request.AddParameter("email", "[email protected]"); 
request.AddParameter("password", "abc123"); 

var response = restClient.Execute(request); 

lblMessage.Text = response.Content; 
+0

我必须使用JavaScript来调用REST服务。 – allDroid

+0

您是否尝试过使用https://www.getpostman.com测试其他服务?我要求你在Chrome中添加扩展并至少在Postman中测试一次。 – Keval

+0

在邮政人的工作正常,但它不工作在asp.net ajax呼叫 – allDroid

相关问题