JSON returned data it is in {d:“data”} format

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?

Answers


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)).


Need Your Help

How to avoid this kind of exception?

c# sql sqlite

I have an sqlite database, and a table, in which i have a column PIN as a TEXT, and it's empty.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.