简体   繁体   English

我的jQuery.ajax()parsererror是什么原因?

[英]What is the reason of my jQuery.ajax() parsererror?

I'm trying to get a JSON from a webservice with javascript $.Ajax call. 我正在尝试使用JavaScript $ .Ajax调用从Web服务获取JSON。

<script type="text/javascript">
    $(function () {
        $("#" + "@Model.BidObjectId").submit(function () {
            alert("Test");
            var param = { 'id': "@Model.BidObjectId" };
            $.ajax({
                url: "http://localhost:11523/Service1.svc/GetBidObject",
                dataType: "jsonp",
                contentType: "application/json;charset=utf-8",
                type: "GET",
                data: JSON.stringify(param),
                success: function (msg) {
                    alert("success");
                    if (msg != null) {
                        return msg.URL;
                    }
                },
                error: function (msg2) {
                    alert(msg2);
                }
            });
            return false;
        });
    });
</script>

I always go in the error scenario with a parsererror 我总是在错误情况下遇到parsererror

status: 200 statusCode: function ( map ) { statusText: "parsererror" 状态:200 statusCode:函数(地图){statusText:“ parsererror”

I already read an explaination here but I cannot use JSON because this create some OPTION call. 我已经在这里阅读了说明但是我不能使用JSON,因为这会创建一些OPTION调用。 I have tried changing the POST to a GET, returning the data in a few different ways (creating classes, etc.) but I cant seem to figure out what the problem is. 我尝试将POST更改为GET,以几种不同的方式(创建类等)返回数据,但是我似乎无法弄清楚问题出在哪里。 Only the solution with JSONP seems to be agreed to do a correct GET or POST. 似乎只有使用JSONP的解决方案才能进行正确的GET或POST。 other solutions dont' even find my webservice. 其他解决方案甚至找不到我的网络服务。

Here is the code of my webservice: 这是我的网络服务的代码:

        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/GetBidObject?id={id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        string[] GetBidObject(string id);

with

        public string[] GetBidObject(string id)
        {
            BidObject bidobject = new BidObject() { BidObjectId = 1, Title = "callback" };

            JavaScriptSerializer ser = new JavaScriptSerializer();
            string result =  ser.Serialize(bidobject);
            List<string> listResult = new List<string>();
            listResult.Add(result);
            return listResult.ToArray(); 
        }

I don't work with ASP.NET but with Razor. 我不使用ASP.NET,但使用Razor。

[EDIT] [编辑]

If I change jsonp by json in Fiddler I can read my call is OPTIONS http://localhost:11523/Service1.svc/GetBidObject?{%22id%22:%220%22} HTTP/1.1 with chrome. 如果我在Fiddler中通过json更改jsonp,我可以看到我的调用是OPTIONS http://localhost:11523/Service1.svc/GetBidObject?{%22id%22:%220%22} HTTP/1.1带有Chrome)。 In IE Fiddler detect nothing. 在IE Fiddler中什么也没发现。 The ajax call is never done... I really don't understand. Ajax调用从未完成...我真的不明白。

JSON and query strings are two different things. JSON和查询字符串是两件事。 You do not need to use JSON when sending data via GET (or POST). 通过GET(或POST)发送数据时,无需使用JSON。

$.ajax({
    url: "http://localhost:11523/Service1.svc/GetBidObject",
    dataType: "jsonp",
    type: "GET",
    data: param,
    success: function (msg) {
        alert("success");
        if (msg != null) {
            return msg.URL;
        }
    },
    error: function (msg2) {
        alert(msg2);
    }
});

jQuery will correctly serailize param and generate the URL: jQuery会正确地遍历param并生成URL:

http://localhost:11523/Service1.svc/GetBidObject?id=123&callback=jquery1234

Then you can just read the id value from the query string in your backend. 然后,您可以从后端的查询字符串中读取id值。

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

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