$("#1").click(function () {
$.ajax({
url: "api/DynamicTest/Test",
type: "post",
contentType: 'application/json;charset=utf-8',
data:JSON.stringify({"name":"jim","age":"12"}),
success:function(data)
{
alert(data);
},
dataType:"json"
})
})
//隐藏内容
$("#hideContent").click(function () {
if ($("#hideContent").val() == "显示上文")
{
$("#content").show();
$("#hideContent").val("隐藏上文");
}
else
{
$("#content").hide();
$("#hideContent").val("显示上文");
}
})
//测试通过
$("#2").click(function () {$.ajax({
url: "api/DynamicTest/Test2",
type: "get",
contentType: 'application/json;charset=utf-8',
data: { strQuery: JSON.stringify({ "name": "jim", "age": "12" })},
dataType:"json",
success: function (data) {
alert(data);
}
})
})
//这个get中,data的写法必须是要有值,而且后台Test2(string name)参数名要与key值一样
$("#2").click(function () {
$.ajax({
url: "api/DynamicTest/Test2",
type: "get",
data: { "name": "jim" }, // data:{"name":"jim","age":"123"} Test2(string name,string age) get 是不能用结构体的
success: function (data) {
alert(data);
}
})
})
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
//using System.Web.Mvc;
using WEBapi.Controllers;
using WEBapi.Models;
namespace WEBapi.Models
{
public class DynamicTestController : ApiController
{
//
// GET: /DynamicTest/
[HttpPost]
public string Test(dynamic X)
{
return X.name + "hell";
}
[HttpGet]
public string Test2(string strQuery)
{
Class1 str = Newtonsoft.Json.JsonConvert.DeserializeObject<Class1>(strQuery);
return "hello"+str.name;
}
}
}