Creating JSON returns "strings" from a web service for use with jquery ajax

I tried embedding a simple web service in an asp.net application using the tutorial found here: http://dotnetslackers.com/articles/ajax/JSON-EnabledWCFServicesInASPNET35.aspx#1301 and http://dotnetslackers.com/articles/ajax /Using-jQuery-with-ASP-NET.aspx

The problem is that my data is being returned as shown in this screenshot (according to Firebug): alt text

    $("#btnGet").click(function () {

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "TimeService.svc/GetCar",
            data: "{}",
            dataType: "json",
            success: function (data) {
                alert(data.d);
            }
        });

    });

});

My web service method is as follows:

[OperationContract]
public string GetCar()
{
    using (var sqlc = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CarTracker.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"))
    {
        sqlc.Open();
        var cmd = sqlc.CreateCommand();
        cmd.CommandText = "SELECT CarID, CarName FROM tblCars";
        using (var reader = cmd.ExecuteReader())
        {
            string sCar = "";
            int testcount = 1;
            for (int i = 0; i < testcount; i++)
            {
                reader.Read();
                sCar += reader["CarName"].ToString();
            }


            return sCar; // Car_1
        }
    }
}

So my questions are:

  • Where is the "d" in firebug from?

  • How do I create a JSON style? "strings" based on my db to return back to jquery ajax function?

Ideally, I would like the jquery ajax data to look something like this:

{"TotalCars": x, "CarList":[{"CarName":"x1", "CarID":"id1"},{"CarName":"x2", "CarID":"id2"}]} 

, jquery , alert(data.TotalCars); .

, , , , . ! < 3

+3
2

"d" webservice, , . , Javascript-.

, , :

[DataContract]
public class CarCollection {
    [DataMember]
    public int TotalCars { get { return CarList.Count; }}
    [DataMember]
    public List<Car> CarList { get; set; }
}

[DataContract]
public class Car {
    [DataMember]
    public string CarName { get; set; }
    [DataMember]
    public string CarId { get; set; }
}

. WCF HTTP GET JSON WebGet:

[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json)]
public string GetCar()
{
    // You will probably build this up from your databas
    var cars = new CarCollection { CarList = new List<Car>() {
        new Car { CarName = "x1", CarId = "id1" },
        new Car { CarName = "x2", CarId = "id2" },
        new Car { CarName = "x3", CarId = "id3" },
    }};

    return cars;
}

WCF JSON .

JQuery get:

$("#btnGet").click(function () {
    $.get("TimeService.svc/GetCar", function(data){
        alert(data);
    });
});
+6

1) "d" JSON ASP.Net, javascript, javascript .

2) Webservice .Net- JSON, - [ScriptMethod(UseHttpGet=true, ResponseFormat=ResponseFormat.Json)]. CarCollection @joshperry, :

[WebMethod()]
[ScriptMethod(UseHttpGet=true, ResponseFormat=ResponseFormat.Json)]
public CarCollection GetCars()
{
    CarCollection carResult = new CarCollection();
    ...
    return carResult;
}
+2

Source: https://habr.com/ru/post/1779087/


All Articles