Returning a value to the page from a jquery script

Sherry Collins asked on June 1, 2015 16:52

I have implemented a "Likes" button on my blog post pages that functions much like the "Comments" button. I have created a jquery that calls a webmethod that calls a stored procedure to input a record into the database and return the count of likes for that document. All is well up to this point.

Before the user clicks the Likes button the UI will look like this: 6 Likes After the user clicks the "Likes" button I need to return the count from the jquery so that it now looks like this: 7 Likes

My markup looks like this:

<span id="newlikes" data-currentuser="<%# BlogFunctions.GetUserName(Eval("CurrentUser")) %>" data-blogposttitle="<%# Eval("BlogPostTitle")%>">Like

My jquery looks like this

$(document).ready(function () {

  $('#newlikes').click(function () {
    var result="";
           $.ajax({
               type: "POST",
               url: "/CMSPages/WebService.asmx/InsertLikes",

                 data: { UserID: $(this).data("currentuser"), DocumentID: $(this).data("blogposttitle") },
                   dataType: "json",
                   contentType: "application/xml; charset=utf-8",
                   async: false,  
                   success: function (data) {                   
                    result = data;
                            alert(result); ******I need to get this value written to the page
                        },

            });
        });


        });

Can someone help me with the jquery to post the value back to the #newlikes div.

Thanks.

Correct Answer

Richard Sustek answered on June 2, 2015 10:14

Hi Sherry,

You are right about the JSON - try to make your web service to return JSON instead and then use

JSON.parse()

method to parse the properties you want to display. You can find more information about parsing here and here

Kind regards,

Richard Sustek

1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on June 1, 2015 17:53

What does alert(result) display in your alert box?

If it's displaying what you need, then you can simply take that value and assign it to the div like so $('#newlikes').text(result);

0 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on June 1, 2015 21:58 (last edited on June 1, 2015 21:59)

This is what I get back in my alert box, or more better I am getting this from Fiddler.

<?xml version="1.0" encoding="utf-8"?>

string xmlns="http://tempuri.org/">8/string

When I try $('#newlikes').text(result); I get [object XMLDocument] instead of 8 Likes

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 2, 2015 02:09

You might have to try pasting your code again, looks like the editor got the best of it. Use the < / > in the toolbar to add a code block

0 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on June 2, 2015 02:24

When I run Fiddler on this, it returns

<?xml version="1.0" encoding="utf-8"?>

<string xmlns="http://tempuri.org/">8</string

When I try $('#newlikes').text(result); I get

[object XMLDocument] - I think this is because I am using the wrong content type. When I go with the default content type I get the returned value wrapped in xml. Same thing Fiddler shows. I would like to use json, but I am getting an error that my parameters are not valid JSON . Looking at Fiddler it is throwing an error at $(this).

Thanks.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.