ajax get 的写法
2016-06-20 13:55
351 查看
<script type="text/javascript">
$("#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;
}
}
}
$("#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;
}
}
}
相关文章推荐
- Linux Ubuntu 内核升级
- poj 2236 并查集水题
- gen already exists but is not a source folder. Convert to a source folder or rename it
- 欢迎使用CSDN-markdown编辑器
- Activity生命周期
- LXT6架构-ARM公司GPU系列性能变迁(2016版)
- Intellij IDEA 配置 JavaWeb开发环境(Tomcat 配置)
- PE中的配置文件定义
- EasyUI datetimebox 时间格式化
- CodeForces 682C Alyona and the Tree 搜索
- iOS—简单封装UITableView使用1
- 从租售比看中国房价的合理性
- hdu 1021 Fibonacci Again
- 导入maven不能选择install而应该选择Imort-Maven-Existing maven projects
- 算法基础-最大公约数
- 职场感悟一
- if逻辑判断中 grep -q 的用法
- MFC各种控件属性介绍
- textarea value显示的值在2个标签中间;
- 企业级 Linux 安全管理实例(1)