0

I create a new WebForms Application in VS2013. I didn't change anything and created a simple page. I want to load this table on client side when user clicks button

<table id="tblHelpRow">
        <thead>
            <tr class="title">
                <th>F2
                </th>
                <th>F3
                </th>
            </tr>
        </thead>
        <tbody id="helpRowBody">         
            <%=MyRow %>  
        </tbody>
    </table>
<asp:LinkButton ID="lnkEdit" runat="server" onclick="fnEdit();" />

This is my script code:

function fnEdit() {
    BindGridView();
};
function BindGridView() {
    rowid = {rowID:2};
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetRow",
            contentType: "application/json; charset=utf-8",
            data: param,
            dataType: "json",
            success: function (data) {
                 alert(data);
            }
        });
}

I have a WebMethod in my code-behind which stores result in the public property. DataSource I store in session, but rowID I need to pass from jquery.

        [WebMethod]
        public static string GetRow(int rowID)
        {
                DataTable dt = (DataTable)HttpContext.Current.Session["SourceData"];
                MyRow = "<tr>" +
                "<td>" + dt.Rows[rowID]["F2"].ToString() + "</td>" +
                "<td>" + dt.Rows[rowID]["F3"].ToString() + "</td>" +
                "</tr>";
            return "done";
    }

But I don't get any result. When I have put the breakpont in success I got "Authentication failed" error and this webmethod wasn't executed. What's the problem? I didn't change ant Authentication settings.

0

3 Answers 3

1

Try removing the ScriptMethod attribute. You are specifying a POST action type but I believe the ScriptMethod forces the request to be a GET by default. Also, I believe your param needs to be a JSON string instead of just an integer:

var param = {rowID:2};
$.ajax({
    type: "POST",
    url: "Default.aspx/GetRow",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(param),
    dataType: "json",
    success: function (data) {
         alert(data);
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Can you throw a debug breakpoint in the web method and see if it's even hitting it?
Just threw this together myself. Definitely need the param in JSON format. The ScriptMethod didn't matter. I had to comment out the MyRow because I am not doing anything with that and I didn't build and store the datatable to session so it's always null. But, it does return "done" and enters the WebMethod. I am also assuming your markup is "OnClientClick" and you're preventing the postback - sample above is not that way.
Thanks... Looks like the problem in my VS. I will try recreate this project and try again
1

In my VS2013 web forms project the culprit turned out to be:

var settings = new FriendlyUrlSettings {AutoRedirectMode = RedirectMode.Permanent};

Using default settings for FriendlyUruls solved the problem--do not use RedirectMode.Permanent.

The ajax call like this, where the data params are complex.

    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: applicationBaseUrl + "mypage.aspx/Calc",
        data: params     // data is json
    }).success(function (data, status, xhr) {
        //...
    }).error(function (xhr, status, error) {
        //...
    });

The WebMethod like this

   [WebMethod]
    public static string Calc(IEnumerable<AiApp> aiApps, Guid guid)
    { //...

Comments

1

Use

$.ajax({
        type: "POST",
        url: "Default.aspx/GetRow",
        contentType: "application/json; charset=utf-8",
        data: {rowID:2},
        dataType: "json",
        success: function (data) {
             alert(data);
        }
    });

1 Comment

Give more explanation instead of giving in single keywords

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.