MVC中使用ajax和json传递数据简单示例

1.jquery中ajax的简单使用方法。

  传输数据与内容:
  传输数据时:json要解析。

    function GetValues() {
        $.ajax({ 
            url: "GetJson",//请求地址
            type: "Post",//请求方式:GET,Post
            datatype: "json",//传递数据的json
            timeout: 1000,
            success: AlertInfo  //回调函数 
        });
    }
 function AlertInfo(result) {
        alert(result.randfloat);
        alert(result.readarray);
    }

Controller中的方法,用this.Json(object data)

public ActionResult GetJson()
        {
            Random rand = new Random(DateTime.Now.Millisecond);
            return this.Json(new { randfloat=rand.NextDouble(),
                readarray=new int[]{rand.Next(),rand.Next(),rand.Next()}
            });
        }

2.  传输内容:传输html内容。

原文地址:https://www.cnblogs.com/netlove/p/2988842.html