简体   繁体   English

网页(.cs)中编写的javascript中的调用函数

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

I have function on .cs page 我在.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;
}

My Html page code is 我的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>

I am trying to call my web side function to client side on submit click. 我试图在提交点击时将我的Web端函数调用给客户端。 Am I missing something? 我想念什么吗? Please give a demo from my above code 请从我上面的代码中进行演示

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() 
{ 

On your url try : "PageName.aspx/MethodName". 在您的网址上尝试:“ PageName.aspx / MethodName”。 Also check out this blog post by Dave Ward : 另请参阅Dave Ward的这篇博客文章:

Using jQuery to directly call ASP.NET AJAX page methods 使用jQuery直接调用ASP.NET AJAX页面方法

Below line is error prone. 下面的行容易出错。 do not include "()" in url method name. 网址方法名称中不要包含“()”。

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

Replace above line with 将上面的行替换为

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

See the following working example 请参阅以下工作示例

// Code behind method declared static

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

your button whose click this has to be done 您的按钮,必须单击此按钮

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

script for this 为此脚本

<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"); }
 });
};

From your comments I have gathered that you are verifying if this method is working by checking for new entries in a database table. 从您的评论中,我收集到您正在通过检查数据库表中的新条目来验证此方法是否有效。 The data in the database could be missing for other reasons rather than the query. 数据库中的数据可能由于其他原因而不是查询而丢失。 To verify, try a more simple web method, and go from there. 要进行验证,请尝试使用更简单的网络方法,然后从那里开始。

eg, 例如,

Html : HTML:

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

Javascript : 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# C#

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

If you do not see a success response, then the problem is indeed with your javascript, or rather with the site setup which is somehow preventing callbacks from javascript. 如果您没有看到成功的响应,则问题确实出在您的javascript上,或者是网站设置,从而以某种方式阻止了javascript的回调。

If the method succeeds then it is likely that your database insertion script is raising an error and you should step through it to see that cause. 如果该方法成功,则您的数据库插入脚本可能会引发错误,您应该逐步检查该错误以查看原因。

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

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