简体   繁体   中英

Passing GUID to WebMethod from jQuery

I have a jquery function that is calling a C# webmethod. I'm trying to pass a GUID to the WebMethod but I keep receiving an error. The only detail I'm getting back is that the input string was not formatted correctly.

The GUID is passed into my jquery function as a string. It is then passed to a WebMethod via post data. It is saved to the database as a string in a field that is nvarchar(MAX).

My jquery function looks like this:

function SaveLike(ID_IN) {

    var jsonText = JSON.stringify({ ID: ID_IN });
    var lblID = "#lblLike" + ID_IN;

    $.ajax({
        type: "POST",
        url: "http://test.mainehousing.org/Services/ActivityFeedService.aspx/SaveLike",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: jsonText,
        success: function (msg) {
            $(lblID).text(msg.d);
        },
        error: function (xhr, status, error) {
            alert(error);
        }
    });
}

and my C# WebMethod looks like this:

[System.Web.Services.WebMethod()]
    public static string SaveLike(string ID_IN)
    {
        //
        ActivityFeed.clsFeed objAF = new ActivityFeed.clsFeed();
        ActivityFeed.clsFeeds objAFS = new ActivityFeed.clsFeeds();
        int i;
        string oLikes = "";
        string oSTR;

        objAF = new ActivityFeed.clsFeed();
        objAF.SubFeedItemID = ID_IN;
        objAF.SubFeedEmployeeID = HttpContext.Current.Session["globalEmpID"].ToString();
        objAF.SubFeedIndicatorID = 1;
        objAF.SubFeedComment = "";
        objAF.SubFeedItemDate = DateTime.Now;
        objAF.SaveActivityFeedLike();
    }

I've been trying to figure this out for days and am really stumped. Anyone have any ideas? Any help would be greatly appreciated.

Thanks Amanda

Btw is it a C# error or a AJAX error? only issue i noticed is name of your web method parameter... Hope your web method should be like following

[System.Web.Services.WebMethod()]
public static string SaveLike(string ID)
{
    //
    ActivityFeed.clsFeed objAF = new ActivityFeed.clsFeed();
    ActivityFeed.clsFeeds objAFS = new ActivityFeed.clsFeeds();
    int i;
    string oLikes = "";
    string oSTR;

    objAF = new ActivityFeed.clsFeed();
    objAF.SubFeedItemID = ID_IN;
    objAF.SubFeedEmployeeID = HttpContext.Current.Session["globalEmpID"].ToString();
    objAF.SubFeedIndicatorID = 1;
    objAF.SubFeedComment = "";
    objAF.SubFeedItemDate = DateTime.Now;
    objAF.SaveActivityFeedLike();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM