Alfresco登录api给出Bad Request 400

时间:2014-10-16 07:52:38

标签: ajax json alfresco bad-request

我正试图通过api登录露天。我不知道为什么它返回错误400 Bad Request。 json应该是正确的,在我的ajax调用中,我也将内容类型设置为'application / json'。

这是我的ajax电话。

var jsonData = JSON.stringify({ username : usernameV, password : passwordV });

var request = $.ajax({
    settings : {contentType:'application/json'},
    type: "POST",
    url: "http://---ip---/alfresco/service/api/login",
    data: jsonData
});

控制台中的json字符串。

{"username":"admin","password":"admin1"} 

错误

400 Bad Request 
Request sent by the client was syntactically incorrect

responseJSON对象中的消息

Unable to parse JSON POST body: A JSONObject text must begin with '{' at character 0"

2 个答案:

答案 0 :(得分:2)

我怀疑这与您设置contentType的方式有关,因为发生这种情况的唯一方法似乎是空JSON或不正确的contentType。尝试:

var request = $.ajax({
    contentType:"application/json",
    type: "POST",
    url: "http://---ip---/alfresco/service/api/login",
    data: jsonData
});

答案 1 :(得分:2)

我已经创建了一个同样的东西的java程序。我认为你应该在url.Even中传递用户名和密码,如果你直接在浏览器中点击url,它会给你alf_ticket,这在alfresco的身份验证中是完整的

private static String getAlfticket() throws IOException, JSONException {
        String ticket = "";
        URL url = new URL("http://hostname/alfresco/service/api/login u="+USERNAME+"&pw="+PASSWORD+"&format=json");
        URLConnection con = url.openConnection();
        InputStream in = con.getInputStream();
        String encoding = con.getContentEncoding();
        encoding = encoding == null ? "UTF-8" : encoding;
        String json = IOUtils.toString(in, encoding);
        JSONObject getData = new JSONObject(json);
        System.out.println(getData.getJSONObject("data").get("ticket")
                .toString());
        ticket =getData.getJSONObject("data").get("ticket").toString();
        return ticket;
} 


Krutik Jayswal
Alfresco开发人员