jQuery ajax返回[object Error]

时间:2014-02-26 22:58:07

标签: javascript jquery ajax rest

我有一个调用API的HTML页面,但每次尝试运行时,我都会收到[object Error]

这是我的代码:

jQuery.support.cors = true;
jQuery.ajax({
    type: "POST",
    url: "https://xxxx:8440/ora/authenticationService/authentication/signIn",
    contentType: "application/json",
    dataType: "json",
    data: "{'requestParameters':{'username':'xxxx', 'password':'xxxx'}}",
    success: function (data, status, jqXHR) {
        alert('exito!');
    },
    error: function(jqXHR, status, errorThrown) {
        alert("Falla : " + errorThrown);
    }
});

有关此错误的任何想法?

3 个答案:

答案 0 :(得分:0)

最可能的解释是,当您告诉它您正在发送JSON但是您的JSON无效时,服务器会抛出错误。

您不能使用'来分隔JSON中的字符串。

不要手动编写JSON,构造JavaScript对象,然后使用JSON序列化库。

data: JSON.stringify({requestParameters:{username:'xxxx', password:'xxxx'}),

答案 1 :(得分:0)

我认为jQuery.support.cors = true;仅用于检查浏览器是否支持cors,而不是设置它。

也许尝试使用JSONP而不是JSON。 JSONP专为跨域数据请求而设计。

答案 2 :(得分:0)

已经找到了我的项目的解决方案但是不能用JQuery做,与.NET

有关

以下是代码片段,如果有人帮助的话:

    WebRequest request = WebRequest.Create("https://xxxxxx/ora/authenticationService/authentication/signIn");
    request.Method = "POST";

    string postData = "{\"requestParameters\":{\"username\" : \"xxxx\", \"password\":\"xxxx\"}}";
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);

    request.ContentType = "application/json";
    request.ContentLength = byteArray.Length;

    System.Net.Security.RemoteCertificateValidationCallback valor = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
    ServicePointManager.ServerCertificateValidationCallback = valor;

    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    WebResponse response = request.GetResponse();
    dataStream = response.GetResponseStream();

    StreamReader reader = new StreamReader(dataStream);

    string responseFromServer = reader.ReadToEnd();

    HttpContext httpContext = HttpContext.Current;
    NameValueCollection headerList = response.Headers;  

    HttpContext.Current.Session["JSESSIONID"] = headerList.Get("Set-Cookie");


    reader.Close();
    dataStream.Close();
    response.Close();

感谢所有人!