使用jquery ajax方法从客户端调用Web方法 - 不调用方法而是进入页面加载

时间:2013-09-14 08:07:40

标签: asp.net ajax jquery client-side-scripting

我单击我的编辑按钮,它在javascript中输入按钮点击功能。当执行ajax()时,控件转到WebForm的页面加载,而不是直接调用webform的update(),我提到[url:“ aFax()中的WebForm1.aspx / update“,”。为什么不调用我的更新函数?     我有我的webform的母版页,按钮是asp:control

This is my javascript
$(document).ready(function () {

    $("input[id*='edit']").click(function () {
        var userdata = {};
        userdata["Name"]="Saran";
        var DTO = { 'userdata' : userdata};
        $.ajax({
            type: "POST",
            contenttype: "application/json; charset=utf-8",
            url: "WebForm1.aspx/update",
            data: JSON.stringify(DTO),
            datatype: "json",
            success: function (result) {
                //do something
                alert("SUCCESS = " + result);
                console.log(result);
            },

            error: function (xmlhttprequest, textstatus, errorthrown) {
                alert(" conection to the server failed ");
                console.log("error: " + errorthrown);
            }
        });//end of $.ajax()

        return false;
    });//eof button click
});


and my webform looks like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Payoda.Assignments.BusinessLayer;

namespace Payoda.Assignments.LoginApplication
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [WebMethod]
        public static string update(string userdata)
        {
            return "success";
        }
    }
}


my aspx page looks like this

<%@ Page Title="" Language="C#" MasterPageFile="~/MyMasterPage.Master" AutoEventWireup="true" CodeBehind="TrialPage.aspx.cs" Inherits="Application.TrialPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="Scripts/jquery-1.10.2.js"></script>
    <script src="Scripts/TrialScript.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Contents" runat="server">
    <asp:Button ID="editLink" runat="server" Text="MY BUTTON" />
</asp:Content>

2 个答案:

答案 0 :(得分:0)

我发现了错误...... 其中一个$ .ajax参数是contentType,它被错误地写为contenttype,而正确的格式是“contentType”......

答案 1 :(得分:0)

你应该使用常规的html输入而不是asp:button