开发者

posting string data back via webservice using ajax and jquery

开发者 https://www.devze.com 2023-03-31 21:14 出处:网络
all, I\'m trying to pass html data up to a webservice that exists in .NET/c#.I\'m using JQUERY but continue to get an error of \"failed posted data\".Is my syntax wrong in either my ajax script or in

all,

I'm trying to pass html data up to a webservice that exists in .NET/c#. I'm using JQUERY but continue to get an error of "failed posted data". Is my syntax wrong in either my ajax script or in my code behind? Thanks for any help.

$(function () {

    $("[id$=rdbSaveAjax]").click(function () {  

    var markedUPHTML = $("[id$=wrapper]").html();
    var postData = "{'HTMLData' : '" + markedUPHTML + "'}";

    $.ajax({
            type: "POST",
            url: "Role.asmx/test",
            data: JSON.stringify({html: postData}),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert("successfully posted data");
            },
            error: function (data) {
                alert("failed posted data");
                alert(JSON.stringify(开发者_JAVA百科postData));
            }

        }); 
    });

});

webservice code behind:

    /// <summary>
    /// Summary description for Role
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class Role : System.Web.Services.WebService
    {
    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public string test(string html)
    {

        string strHTMLData = html;
        return html;

    }

}


var postData = "{'HTMLData' : '" + markedUPHTML + "'}";

This will not build a proper string if markedUPHTML has single or double quote in it. Use JavaScript escape method before concatenating. Try this

var postData = "{'HTMLData' : '" + escape(markedUPHTML) + "'}";

And then while passing the data you dont have to strigify it because its already a string. Just try this

 data: { html: postData }


Try public static string on the server side, instead of public string


all,

I ended up finding the issue by using firebug. Everytime I tried posting back to connect to the test method in my Role.asmx file, I would get a 500 error. My solution is a Cassini project so I could not do much with configuring my local IIS site to change permissions.

Instead, I created a method in my code behind and placed [WebMethod] right above the method name. In my code behind I did not setup any declarations that you would typically see after adding a webservice file to your solution.

[WebMethod] public static bool test(string strHTMLMarkup) { ....code }

I hope this helps anyone other there.

Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消