ASMX webservice - return JSON instead of XML
Asked Answered
A

1

6

I have a web service that contains one method:

[WebMethod]
public string Movies()
{
    using (var dataContext = new MovieCollectionDataContext())
    {
        var query = dataContext.Movies.Select(m =>new{m.Title,m.ReleaseDate}).Take(20);
        var serializer = new JavaScriptSerializer();
        return serializer.Serialize(query);
    }
}

The method properly serializes the object, but when I view the response in FireBug, it looks like this:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">[{"Title":"SQL","ReleaseDate":"\/Date(1224007200000)\/"},{"Title":"Termonator Salvation","ReleaseDate":"\/Date(1224007200000)\/"}]</string>

Here is jQuery method in which I use Kendo Data Source

$(function () {
    alert("Welcome To Kendo");
    var dataSource = new kendo.data.DataSource(
                {
                    transport: {
                        read: {
                            type: "POST",
                            dataType: "json",
                            url: "/MovieService.asmx/Movies"
                           // contentType: "application/json; charset=utf-8"

                        }
                    },
                    change: function (e) {
                        alert(e);

                    },
                    error: function (e) {
                        alert(e[2]);
                    },
                    pageSize: 10,
                    schema: {
                        data: "d"

                    }


                });

    $("#MovieGridView").kendoGrid({
        dataSource: dataSource,
        height: 250,
        scrollable: true,
        sortable: true,
        pageable: true,
        columns: [
            { field: "Title", title: "Movie Name" },
            { field: "ReleaseDate", title: "Movie Release" }
            ],
        editable: "popup",
        toolbar: ["create"]
    });
});

The above code show what I am doing in jQuery and when the error event call I got this error

SyntaxError: JSON.parse: unexpected character

How can I convert the above data into JSON so I can use it in jQuery? And where am I going wrong?

Autonomic answered 28/3, 2012 at 20:1 Comment(4)
Why are you using JSONP here? It doesn't make sense.Afterburner
ok i remove jsonp but problem is still same plz solve itAutonomic
i found another way of doing this by using WCF and JSONP its very cool combination and work perfectlyAutonomic
you need json with xml or only jsonBelike
A
8

You need to specify the ResponseFormat of the method:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetMovies() {
}

Note: For the sake of others who arrive at this question with similar issues, it's also important to note that you should being using POST requests, not GET requests. See: JSON Hijacking and How ASP.NET AJAX 1.0 Avoids these Attacks


EDIT

Based on the jQuery that you posted, you're not calling the correct method. You C# defines a method called GetMovies, yet your jQuery is attempting to call a method called `Movies'.

This:

url: "/MovieService.asmx/Movies"

Should change to this:

url: "/MovieService.asmx/GetMovies"
Acquit answered 28/3, 2012 at 20:4 Comment(6)
@HaseebKhan, first of all, don't call me dear :-). Secondly, this is the proper way to return JSON formatted data. Please review my answer, and confirm that you've implemented it properly in your test environment.Acquit
see my updated question so its clear to you that what i am doing and where i going wrong! and sorry that i call u dear.Autonomic
yes yes offcourse i remove the get and write this line also [ScriptMethod(ResponseFormat = ResponseFormat.Json)] but still my problem is not solvedAutonomic
but the problem is still sameAutonomic
@HaseebKhan, as you can tell by the upvotes, based on the info you've provided, this is the correct answer. Either provide more information, or take a second, or third, look at your code.Acquit
I have the same problem; it's bringing back xml-encapsulated json.Descender

© 2022 - 2024 — McMap. All rights reserved.