簡體   English   中英

網頁(.cs)中編寫的javascript中的調用函數

[英]calling function in the javascript written in webpage(.cs)

我在.cs頁面上具有功能

[System.Web.Services.WebMethod]
    public static string getdata()
{
    ProductBAL objbal = new ProductBAL(); // Calling class
    int i = 0;
    i = objbal.get_last_orderid(); //Select query
    i = i + 1;
    ProductDAL objdal = new ProductDAL(); // Calling class
    objdal.insert_new_orderid(i); //Insert query
    HttpCookie orderid = new HttpCookie("orderid");
    orderid.Value = "MP_" + Convert.ToString(i);
    Response.Cookies.Add(orderid);
    Response.Cookies["orderid"].Expires = DateTime.Now.AddHours(5);
    string abc=Convert.ToString(i);
    return abc;
}

我的HTML頁面代碼是

<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>calling function from .cs</title> 
<script language="javascript" type="text/javascript">    
    function Submit1_onclick() {        

        $.ajax({ type: "GET", url: "default.aspx/getdata()", success: function (data) { });

           alert("Done");        
    }
</script>
</head>

<body>
<form name="ecom" method="post" action="https://www.google.co.in/">
<input id="Submit1" type="submit" name="submit" runat="server" value="Submit" onclick="return Submit1_onclick()">
</form>
</body>

我試圖在提交點擊時將我的Web端函數調用給客戶端。 我想念什么嗎? 請從我上面的代碼中進行演示

function Submit1_onclick() {
        // alert("Hello");
        $.ajax({
            type: "GET",
            url: 'demo.aspx/getdata',
            data: "{}",

            //"{character:'M'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                alert(data.d);
                //alert("success");
                alert("This is ajax call:");
            },
            error: function() {
                //alert(Error);
                alert("something went wrong");
            }
        });
       // alert("Done");
    }



[WebMethod()] //U have to declare this method as a web method 
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
public static string getdata() 
{ 

在您的網址上嘗試:“ PageName.aspx / MethodName”。 另請參閱Dave Ward的這篇博客文章:

使用jQuery直接調用ASP.NET AJAX頁面方法

下面的行容易出錯。 網址方法名稱中不要包含“()”。

$.ajax({ type: "GET", url: "/getdata()", success: function (data) { });

將上面的行替換為

$.ajax({ type: "GET", url: "/getdata", success: function (data) { });

請參閱以下工作示例

// Code behind method declared static

[WebMethod]
public static string GetSquare(String value)
{
    return "hello" + value;
}

您的按鈕,必須單擊此按鈕

<input type="button" id="button" value="Chnageurl" onclick="ajaxcall()" />

為此腳本

<script type="text/jscript">

function ajaxcall(e) {

        $.ajax({
        type: "POST",
        url: "Default.aspx/GetSquare",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ value: "test" }),
        dataType: "json",
        success: function (value) {
        alert(value.d);
    },
   error: function () { alert("Ajax Error"); }
 });
};

從您的評論中,我收集到您正在通過檢查數據庫表中的新條目來驗證此方法是否有效。 數據庫中的數據可能由於其他原因而不是查詢而丟失。 要進行驗證,請嘗試使用更簡單的網絡方法,然后從那里開始。

例如,

HTML:

<input id="submit" type="submit" name="submit" value="Submit" onclick="return submitClick();" />

Javascript:

function submitClick() {
    $.ajax({
        type: "POST",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "default.aspx/getdata",
        success: function (data) {
            console.log(data);
            alert("success" + data);
        },
        error: function () {
            alert("something went wrong");
        }
    });
    return false; // Note: the return false will prevent postback
}

C#

    [System.Web.Services.WebMethod]
    public static string getdata()
    {
        return "Hello World!";
    }

如果您沒有看到成功的響應,則問題確實出在您的javascript上,或者是網站設置,從而以某種方式阻止了javascript的回調。

如果該方法成功,則您的數據庫插入腳本可能會引發錯誤,您應該逐步檢查該錯誤以查看原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM