如何将值从javascript发送到服务器端(asp.net)?

Q8Y*_*Q8Y 1 javascript asp.net server-side client-side

在不刷新页面的情况下,从JavaScript(客户端)向服务器(asp.net)发送值的最佳方法是什么?

我想将数据插入数据库,但我不想刷新页面或更改它上面的任何数据.

Mar*_*wan 8

简单方法:

1-将jquery导入到您的页面中
2-在页面的cs文件中创建ur函数,如下所示

     [WebMethod]
    public static string Save(string param1,string param2)
    {
        string result;
        //your code must return somthing string
        return result;
    }
Run Code Online (Sandbox Code Playgroud)

3-在您的aspx页面中创建您的功能

function save(){
var param1 = 'value 1 you need to send to the server should be string';
var param2 = 'value 2 you need to send to the server should be string';
         $.ajax({
                  type: "POST",
                  url: "pagename.aspx/Save",
                  data: "{param1: '"+ param1 +"' , param2 :'"+ param2 +"'}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  async: false,
                  cache: false,
                  success: function(result){
                 //Successfully gone to the server and returned with the string result of the server side function do what you want with the result  

                  }
                  ,error(er)
                  {
                  //Faild to go to the server alert(er.responseText)
                  }
          });
        }
Run Code Online (Sandbox Code Playgroud)

4-单击按钮调用此功能

有关我的代码和脚本的更多问题或描述,我在这里:)