JQuery Ajax对RESTful API的错误请求

时间:2014-03-31 13:43:15

标签: jquery ajax json rest

我正在尝试对我的同事开发的演示网络服务进行身份验证。 如果我尝试从Chrome中的Advanced rest客户端插件调用,则允许跨源资源共享并且工作正常。 但是当我尝试使用代码访问时,我得到500服务器错误

.ajax({ 
        type : "POST",  
        dataType: 'json',
        url: 'CANTPOSTTHISURL/rest/session',
        contentType: "application/json; charset=utf-8",      
        data: JSON.stringify({"username": "admin", "password": "admin" }),
        success: function(response)
        {
            console.log(response); 
        },
        error: function (data, status) {
            console.log( 'Sample of error data:', status );
            console.log("readyState: "+data.readyState+"\nstatus: "+data.status+"\nresponseText: "+data.responseText);
        },
        complete: function(xhr, status ) {
            console.log( 'COMPLETE');
        }   
  });

我得到的错误是:

    OPTIONS CANTPOSTTHISURL/rest/session 500 (Internal Server Error) jquery.js:8475
send jquery.js:8475
jQuery.extend.ajax jquery.js:7930
authenticate wts.js:5
(anonymous function) wts.js:27
jQuery.event.dispatch jquery.js:3045
elemData.handle jquery.js:2721
jQuery.event.trigger jquery.js:2957
(anonymous function) jquery.js:3639
jQuery.extend.each jquery.js:631
jQuery.fn.jQuery.each jquery.js:253
jQuery.fn.extend.trigger jquery.js:3638
H jquery.mobile-1.3.0.min.js:2
I jquery.mobile-1.3.0.min.js:2
jQuery.event.dispatch jquery.js:3045
elemData.handle jquery.js:2721
OPTIONS CANTPOSTTHISURL/rest/session Invalid HTTP status code 500 jquery.js:8475
send jquery.js:8475
jQuery.extend.ajax jquery.js:7930
authenticate wts.js:5
(anonymous function) wts.js:27
jQuery.event.dispatch jquery.js:3045
elemData.handle jquery.js:2721
jQuery.event.trigger jquery.js:2957
(anonymous function) jquery.js:3639
jQuery.extend.each jquery.js:631
jQuery.fn.jQuery.each jquery.js:253
jQuery.fn.extend.trigger jquery.js:3638
H jquery.mobile-1.3.0.min.js:2
I jquery.mobile-1.3.0.min.js:2
jQuery.event.dispatch jquery.js:3045
elemData.handle jquery.js:2721
XMLHttpRequest cannot load CANTPOSTTHISURL/rest/session. Invalid HTTP status code 500 index.html:1
Sample of error data: error wts.js:16
readyState: 0
status: 0
responseText:  wts.js:17
COMPLETE 

和标题:

Request URL:CANTPOSTTHISURL/rest/session
Request Method:OPTIONS
Status Code:500 Internal Server Error
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:CANTPOSTTHISURL
Origin:http://127.0.0.1:8020
Referer:http://127.0.0.1:8020/ppm/index.html
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36
Response Headersview source
Access-Control-Allow-Origin:*
Connection:close
Content-Type:application/json
Date:Mon, 31 Mar 2014 13:24:36 GMT
Server:Wildfly 8
Set-Cookie:GEAR=local-53185956500446bd39000020; path=/
Transfer-Encoding:chunked
X-Powered-By:Undertow 1
ConsoleSearch

我认为这是我发送的数据,但我也尝试过' {"用户名":"管理员","密码&#34 ;:" admin" }'或{"用户名":"管理员","密码":"管理员"但没有任何改变。 一个重要的评论:我只能执行POST或OPTION请求,所以没有jsonp 谢谢

1 个答案:

答案 0 :(得分:-2)

我认为你不需要使用stringify方法。

我正在使用以下格式,它适用于我:

url : "http://localhost:9200/" + eleName + "/_suggest",
type : "POST",
dataType : "json",
ContentType: "application/json; charset=utf-8",
data:"{\"result\":{\"text\":\"" + request.term + "\",\"completion\":{\"size\" :10,\"field\":\"name_suggest\"}}}",

尝试使用JSON格式的String。