繁体   English   中英

错误:无法加载资源:服务器的状态为400(错误请求)

[英]Error:Failed to load resource: the server responded with a status of 400 (Bad Request)

我正在尝试使用asp.net将数据发布到java rest服务。当我尝试调用service时出现错误

加载资源失败:服务器响应状态为400(错误请求)。

请在这个我用谷歌搜索但没有找到解决方案的指导中。我通过添加如下所述的代码使我的服务可以在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应用程序中

      $.ajax({
            crossDomain: true,
            type: 'POST',
            url: "http://localhost:9091/employeeservice/employee/create",
            data: JSON.stringify({
                "id": 10,
                "first_name": "narayan",
                "last_name": "bhat",
                "email": "farah@gmail.com",
                "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的新手。

我建议您至少尝试RestSharp来发布和获得Rest API的响应。 如果要在代码后面使用Rest API,则此方法很有用。

几天前,我已经尝试过以某种方式致电Rest Services,并从服务器收到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", "abc@example.com");
request.AddParameter("password", "abc123");

var response = restClient.Execute(request);

lblMessage.Text = response.Content;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM