ajax函数虽然转到我的webservice但仍会出错

时间:2014-04-24 05:00:44

标签: javascript jquery ajax json

<script type='text/javascript'> 
$(document).ready(function () {
    $("#submit").click(function (e) {
        e.preventDefault();
        var userName = $("#username").val();
        var pwd = $("#password").val();
        authenticate(userName, pwd);
    });
});

function authenticate(userName, pwd) {
    var serviceurl = "http://localhost:8080/Authentication/rest/authentication/info";
    $.ajax({
        url: serviceurl + '/' + userName + '/' + pwd,
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: 'jsonp',
        async: true,
        crossDomain: true,
        jsonp: 'callback',
        jsonpCallback: 'jsonpCallback',
        success: function () {
            alert("Web service call succeeded.");
        },
        error: function (error) {
            alert('ERROR has occurred!');
            alert(JSON.stringify(error))
        }
    })
} 
</script>

提交时我得到了以下结果

Request URL:url/Authentication/rest/authentication/info/0009/Aipl@2009?callback=jsonpCallback&_=1398314791025
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Host:localhost:8080
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Query String Parametersview sourceview URL encoded
callback:jsonpCallback
_:1398314791025
Response Headersview source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Content-Type, Accept, X-Requested-With
Access-Control-Allow-Methods:GET, POST, DELETE, PUT, OPTIONS, HEAD
Access-Control-Allow-Origin:*
Content-Type:application/json
Date:Thu, 24 Apr 2014 04:46:34 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

我可以看到我的json数据作为回应

1 个答案:

答案 0 :(得分:0)

我打赌该网页不是 http://localhost:8080提供的。如果没有,您将提交跨站点脚本违规。

编辑:不,跨网站脚本标题就在那里。您是否发送了Content-Type,Accept和X-Requested-With?

以外的标题

编辑:更疯狂的猜测:您的服务器根本没有设置为执行JSONP。从JSONP返回的数据应如下所示

jsonpCallback({"user":"3349997","iq":120})

或者其他什么。如果它只是数据

{"user":"3349997","iq":120}

然后你应该删除所有JSONP的东西并将其作为(更可靠)JSON处理。

编辑:正确答案,删除所有JSONP内容并将其从POST更改为GET。