开发者

JSON returned data it is in {d:"data"} format

开发者 https://www.devze.com 2023-01-27 20:57 出处:网络
I am trying to get JQueryUI\'s Autocomplete code working with an ASMX web service. I am getting close, but hit yet another wall yesterday. The JSON data is being returned in {d:\"data\"} format (see h

I am trying to get JQueryUI's Autocomplete code working with an ASMX web service. I am getting close, but hit yet another wall yesterday. The JSON data is being returned in {d:"data"} format (see http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx). My data now looks like:

d: "[{"DOTNumber":"001061010","JobTitle":"Project Architect"},{"DOTNumber":"003061005","JobTitle":"Principal Electrical Engineer"}]"

My code is:

$(function() {
    function log(message) {
        $("<div/").text(message).prependTo("#log");
        $("#log").attr("scrollTop", 0);
    }
});

$("#dotmatch").autocomplete({
    source: function(request, response) {
        $.ajax({
            type: "POST",
            url: "/AutoSuggestJSTest/AutoSuggest.asmx/DOTFind",
            contentType: 'application/json',
            dataType: "json",
            data: JSON.stringify({ prefixText: request.term, count: 20 }),
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            },
            success: function(data) {
   开发者_如何转开发             var safe = data;
                response($.map(safe.d, function(item) {
                    return {
                        label: item.JobTitle + "(" + item.DOTNumber + ")",
                        value: item.DOTNumber
                    }
                }));
            }
        });
    },
    minLength: 2,
    select: function(event, ui) {
        log(ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value);
        $get("DOTNumber").value = ui.item.value;
    },
});

The problem lies in the success function.

What is the right syntax to get past the "d" issue?


Your data should look like this:

{"d":[{"DOTNumber":"001061010","JobTitle":"Project Architect"},"DOTNumber":"003061005","JobTitle":"Principal Electrical Engineer"}]}

It appears you are missing quotes around your "d" and you have extra quotes around your array.

Don't eval() your data - this opens you up to more security issues than the d: prevented.. You should have access to JSON.parse() or if not jQuery.parseJSON() (which wraps JSON.parse() if available... depends on your target platform(s)).


This has been an incredibly difficult process, but I finally got it working. There were a number of hurdles: 1) My JSON return string was getting wrapped in an XML blanket, so it would not parse 2) Solving this problem required the contentType: 'application/json' line 3) With that content type, a POST was required. GET would not work 4) POST required putting the data together using the JSON.stringify. I am still not sure about this one, but I found some code somewhere that did it. 5) Data coming back from the POST was prefixed with a "d " (see: http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx) 6) getting access to the data itself required the "eval(data.d)" line.

   $("#dotmatch").autocomplete({
        source: function(request, response) {
            $.ajax({
                type: "POST",
                url: "/AutoSuggestJSTest/AutoSuggest.asmx/DOTFind",
                contentType: 'application/json',
                dataType: "json",
                data: JSON.stringify({ prefixText: request.term, count: 20 }),
                success: function(data) {
                    var output = eval(data.d);
                    response($.map(output, function(item) {
                        return {
                            label: item.JobTitle + "(" + item.DOTNumber + ")",
                            value: item.DOTNumber
                        }
                    }));
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 2
    });

If I ever have this much trouble writing a few lines of code again, I am going to take a very large guage shotgun to my computer!

Bob Jones


If you use a WCF JSON service with the webHttpBehavior (instead of enableWebScriptBehavior), it will not emit the "d"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号