StudentRik
StudentRik

Reputation: 1049

Return Properties from $.ajax call

I am trying to populate a page with an ajax call and only getting the sql statement in my success of the ajax call.

My method is:

$('#news-search').on('click', function (e) {
    e.preventDefault();
    $.ajax({
        type: 'post',
        url: '/news.aspx/NewsSearch',
        data: JSON.stringify({
            title: $('#txt_newsSearch').val()
        }),
        contentType: 'application/json;charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            console.log(msg.d);

        },
        error: function (err, jqXHR) {
            console.log(jqXHR);
            console.log(err);

        }
    });
});

I am calling a webmethod in the code behind:

[System.Web.Services.WebMethod]
public static string NewsSearch(string title)
{
    var obj = new NewsSearch();
    using (var db = new DbDataContext())
    {
        var q = db.News.Where(x => x.NewsTitle.Contains(title)).Select(x => new
        {
            x.Article,
            x.NewsTitle,
            x.PublishDate

        });            
        return q.ToString();  
    }
}

I have made a class called NewsSearch with the properties I want to bring back, I tried to assign then in the select but I couldn't.

How will I get the properties the success call returns?

At the minute it returns this to the console.

SELECT [t0].[Article], [t0].[NewsTitle], [t0].[PublishDate]
FROM [dbo].[News] AS [t0]
WHERE [t0].[NewsTitle] LIKE @p0

Upvotes: 0

Views: 78

Answers (2)

Igor Bendrup
Igor Bendrup

Reputation: 2847

Following code will return NewsSearch object in JSON format:

public class NewsSearchResult
{
    public string Article;
    public string NewsTitle;
    public DateTime PublishDate;
}

public class NewsSearch
{
    public List<NewsSearchResult> Results;
}

[System.Web.Services.WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static NewsSearch NewsSearch(string title)
{
    var obj = new NewsSearch();
    using (var db = new DbDataContext())
    {
        var q = db.News.Where(x => x.NewsTitle.Contains(title)).Select(x => new NewsSearchResult
        {
            Article = x.Article,
            NewsTitle = x.NewsTitle,
            PublishDate = x.PublishDate,
        });            
        obj.Results = q.ToList();  
    }

    return obj;
}

Upvotes: 1

Menelaos Vergis
Menelaos Vergis

Reputation: 3955

You have to serialize the result, otherwise you get the linq query.

[System.Web.Services.WebMethod]
public static string NewsSearch(string title)
{
    var obj = new NewsSearch();
    using (var db = new DbDataContext())
    {
        var q = db.News.Where(x => x.NewsTitle.Contains(title)).Select(x => new
        {
            x.Article,
            x.NewsTitle,
            x.PublishDate

        });            
        JavaScriptSerializer json = new JavaScriptSerializer();
        return json.Serialize(q.ToArray());  
    }
}

Don't forget to include the namespace System.Web.Script.Serialization

Have a look at the codeproject article Example Of JavaScript Serializer And Json String Using WebMethod

Upvotes: 0

Related Questions