简体   繁体   English

无法通过从html页面asp.net调用来加载Javascript Ajax函数

[英]Javascript ajax function does'nt load by calling from html page asp.net

I try to call to javascript function from my asp page, but the call does'nt do anything. 我尝试从我的ASP页面调用javascript函数,但是该调用没有任何作用。 this is my code: 这是我的代码:

   <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master"  
   AutoEventWireup="true" CodeFile="addAdmin.aspx.cs" Inherits="addAdmin" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <script type="text/javascript">
    function addAdmin() {
        var auname = document.getElementById("uname")[0].value;
        var apass = document.getElementById("pass")[0].value;
        $.ajax({
            url: "http://localhost:53236/Handler.ashx?cmd=addAdmin&auname="+ auname + "&apass=" + apass,
            async: true,
            dataType: "html",
            success: function (response) {
                $("#status").html(" <strong>One of the fields are empty..</strong> Please fill it and try again later.");

            }
        } );
    }
</script>
 </asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h1>In order to add an admin for the site, fill out the form below</h1>
<br />    <section  class="status"><div id="status" ></div></section><br />
<br />
Admin Username:<input type="text" id="uname" /><br />
Admin Password:<input type="text" id="pass" /><br />
<button type="submit" onclick="javascript:addAdmin();">Submit</button><br />
</asp:Content>

The function in the handler is working well (I tried to send the parameters manually and it workes fine) But I just can't call my function. 处理程序中的函数运行良好(我尝试手动发送参数,并且工作正常),但是我无法调用函数。

I tried a lot of diffrent ways to call my function like: <a href="javascript:addAdmin" >click her </a> and calling from update panel that using the following code in the server while clicking on asp button Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "addShoe()", true); 我尝试了多种不同的方法来调用我的函数,例如: <a href="javascript:addAdmin" >click her </a>并从更新面板中调用,该过程使用服务器中的以下代码,同时单击asp按钮Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "addShoe()", true);

The Content1 from the master page is just in the head tag. 主页中的Content1只是在head标签中。 So I don't know what is the problem and why I can't call my function 所以我不知道问题出在哪里,为什么我不能调用我的函数

First of all, do not use button[type="submit"] to make AJAX requests. 首先,不要使用button[type="submit"]发出AJAX请求。 With the submit button you need to listen also submit event of the form and to prevent it's default behavior. 使用Submit按钮,您还需要侦听form submit事件并防止其为默认行为。 Sometimes it is a little bit confusing 有时候有点令人困惑

Try to use <a> tag and prevent it's default behavior like this 尝试使用<a>标记并防止其成为默认行为,例如

<script type="text/javascript">
    function addAdmin(event) {

        event.preventDefault();

        var auname = document.getElementById("uname")[0].value;
        var apass = document.getElementById("pass")[0].value;
        $.ajax({
            url: "http://localhost:53236/Handler.ashx?cmd=addAdmin&auname="+ auname + "&apass=" + apass,
            async: true,
            dataType: "html",
            success: function (response) {
                $("#status").html(" <strong>One of the fields are empty..</strong> Please fill it and try again later.");

            }
        } );
    }
</script>

and in the HTML instead of <button> : 并在HTML中而不是<button>

<a href="#" onclick="addAdmin(event);">Submit</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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