Home >php教程 >PHP开发 >Example of ASP.NET jquery ajax passing parameters

Example of ASP.NET jquery ajax passing parameters

高洛峰
高洛峰Original
2016-12-08 10:50:261140browse

The first type: GET delivery

Front-end ajax GET delivery: that is, adding parameters after the requested address, the length of the URL address is displayed, and the security is low

Backend reception: Request.QueryString["Parameter Name"]!

For example:

function LoadBar(id) {
  var project = id;
  var month = $("#txtMonth").val();
  $.ajax({
    type: "GET",
    async: false,
    url: 'GetProjectScore.aspx?project=' + project + '&month=' + month,
    dataType: 'json',
    beforeSend: function () {
    },
    success: function (msg) {
      if (msg) {
        optionBar.xAxis.categories = eval(msg.projectscore.orgname);
        optionBar.series = eval(msg.projectscore.series);
        var t = eval("(" + subtitle + ")");
        optionBar.subtitle = t.subtitle;
        chart = new Highcharts.Chart(optionBar);
      }
    },
    error: function () {
      alert('出错了啦!');
    }
  });

Second type: POST transmission

2.1 String Passing as a parameter

That is, the parameters passed are passed in the form of strings. dataType="text";

Front-end ajax POST delivery: a request to submit form data to the server. The data to be submitted is located in the entity behind the information header.

Background reception: Request.Form["Parameter name"]

For example:

$("input[name=returnfile]").click(function () {
           var returnflag = $(this).val();
           var guid = $(this).prev().html();
           $.ajax({
             type: "POST",
             async: "false",
             url: "returndata.aspx",
             data: "flag=" + returnflag + "&guid="+guid+"",
             success: function () {
             }
           });
         })

Background reception:

string flag=Request.Form["flag"];

string guid=Request.Form ["guid"];

2.2 JSON is passed as a parameter

that is, the parameters passed are passed in the form of json string. dataType="json";

Front-end ajax POST delivery: a request to submit form data to the server. The data to be submitted is located in the entity behind the information header.

Background reception:

StreamReader reader=new StreamReader(Request.InputStream);

string str = reader.ReadToEnd();

For example:

$("input[name=returnfile]").click(function () {
           var returnflag = $(this).val();
           var guid = $(this).prev().html();
           var str = "{'flag':'" + returnflag + "','guid':'" + guid + "'}";
           var json = eval("(" + json + ")");
           $.ajax({
             type: "POST",
             async: "false",
             url: "khdf_returndata.aspx",
 
             dataType:”JSON”
             data: json,
             success: function () {
             }
           });
         })

Background reception:

StreamReader reader=new StreamReader (Request.InputStream);

string str = reader.ReadToEnd();

After reading, newtonsoft does JSON processing

2.3 JSON Passed as a parameter

$(function () {
      $("#btnLogin").click(function () {
        // var username = $("#txtUserName").val();
        // var password = $("#txtPassword").val();
        // var paras = "username=" + username + "&password=" + password;
        // alert(paras);
        var data = {
          username: $("#txtUserName").val(),
          password:$("#txtPassword").val()
        }
        $.post("Home/CheckLogin", data, function (data) {
          alert(data);
        })
      })
    })
   
如果按照2.2post。则必须加上: contentType: "application/json"

Background reception:

[HttpPost]
    public ActionResult CheckLogin(string username,string password)
    {
      string name = username;
      string pwd = password;
      return View();
    }


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn