【问题标题】:calling a web method from clientside using jquery ajax method - not calling method rather going into pageload使用 jquery ajax 方法从客户端调用 Web 方法 - 不调用方法而是进入页面加载
【发布时间】:2013-09-14 08:07:40
【问题描述】:

我单击我的编辑按钮,它在 javascript 中进入按钮单击功能。执行 ajax() 时,控件转到 WebForm 的页面加载,而不是直接调用我提到的 webform 的 update() [url:" ajax() 中的 WebForm1.aspx/update",]。为什么不调用我的更新函数? 我的网络表单有母版页,按钮是 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>

【问题讨论】:

  • 请参阅answer,了解如何避免对您的 asp:control 按钮进行回发。
  • @LeonStafford 我想在服务器端调用一个特定的方法,而不是经历完整的asp循环。
  • 我发现我提到的错误是“contenttype”而不是“contentType”。谢谢您的建议
  • 很高兴你找到了它。所以这应该在我相信的 JavaScript 控制台中被选中?在 SO 上发布之前检查任何控制台错误并进行前端/后端的常规调试总是好的,但很高兴看到你加入并希望看到你的更多问题和答案!

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多