Using backbone.js to get data from ASMX with the &

2019-05-25 02:27发布

问题:

I'm pretty new to Backbone and I've been trying to create autocomplete functionality with it, fed by an ASMX webservice. The issue I seem to have is whilst my webservice returns in JSON (after a painful battle with it), it wraps the response in a 'd' (dataset). How do I get the view to understand this and get at the correct data?

Here is my code:-

var Airline = Backbone.Model.extend({
                initialize: function () {},
                defaults: {
                    name: 'Airline Name',
                    rating: 50,
                    icon: '/blank.png'
                }
            });

            var AirlineCollection = Backbone.Collection.extend({
                model: Airline,
                contentType: "application/json",
                url: '/ControlTower/public/runway.asmx/all-airlines',
                parse: function (response) {
                    return response;
                }
            });

            var SelectionView = Backbone.View.extend({
              el : $('#airline'),
              render: function() {
                $(this.el).html("You Selected : " + this.model.get('AirlineName'));
                return this;
              },
            });

var airlines = new AirlineCollection();
            airlines.fetch({async: false, contentType: "application/json" });
            var airlineNames = airlines.pluck("AirlineName");

$("#airline").autocomplete({
              source : airlineNames,
              minLength : 1,
              select: function(event, ui){
                var selectedModel = airlines.where({name: ui.item.value})[0];
                var view = new SelectionView({model: selectedModel});
                view.render();
              }
            });

Can anyone see what I'm doing wrong? I've been sitting here for too long now!

Help is appreciated ;)

回答1:

What about in your AirlineCollection:

parse: function (response) {
  return response.d;
}


回答2:

This works for me

In AirlineCollection

parse: function (response) {
    var data = (typeof response.d) == 'string' ? eval('(' + response.d + ')') :    response.d;
    return data;        
}