如何将json对象发送到C#web服务器
How to send json object to C# web server
我编写了这个java脚本来将json对象发送到c#web服务。但它不起作用。。为什么?这是我的javascript。。
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery- 1.5.1.js"></script>
<script type="text/javascript">
function BindJson() {
document.getElementById("demo").innerHTML=Date();
$.ajax({
type: "POST",
url: "Service1.asmx/SerializeJson",
data: JSON.stringify({ person:{ firstName: "Denny" }}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data2) {
alert(data2.d);
},
error: function (request, status, errorThrown) {
alert(status);
}
});
}
</script>
这是我的服务器类。。
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string SerializeJson(Person person)
{
return "Success";
}
public class Person
{
public string firstName { get; set; }
}
}
您不应该使用JSON.stringify,因为当您指定JSON的内容类型时,jQuery将使用JSON.stringify.进行转换
data: JSON.stringify({ person:{ firstName: "Denny" }}),
contentType: "application/json; charset=utf-8",
dataType: "json",
将其更改为
data: { person:{ firstName: "Denny" }},
contentType: "application/json; charset=utf-8",
dataType: "json",
此外,除非需要,否则您不需要将人员作为对象的成员发送。
data: { firstName: "Denny"},
contentType: "application/json; charset=utf-8",
dataType: "json",
.ajax
的data
选项需要名称-值对字符串或对象
data: { "myjson": JSON.stringify({ person:{ firstName: "Denny" }}) },
//OR
data: "myjson="+JSON.stringify({ person:{ firstName: "Denny" }}),
//Or just send the data values and retrieve in the way you get GET or POST variables in C#
data: { person:{ firstName: "Denny" }},
相关文章:
- node.js将字符串转换为要上传到Web服务器的文件
- 根据HTML文件名授予web服务器写入权限
- 从Web服务器下载图像按钮单击使用JavaScript
- 使用c#在Web服务器上运行JavaScript方法
- 我如何设置HTTP头“;访问控制允许起源”;用于来自thttpd web服务器的HTTP响应
- 用于在web服务器中上载的文件路径
- Web服务器意外退出,正在重新启动新实例
- PhantomJS与嵌入式web服务器只使用一个CPU
- 错误301在web服务器上永久移动
- jQuery插件在本地主机上运行良好,但在web服务器上却与bg镜像混淆
- 如何确保只有您的javascript才能连接到您的Web服务器
- 从本地HTML / JavaScript(没有Web服务器)访问Google API
- 如何在 Javascript 函数中拦截 Web 服务器响应
- 用于嵌入式设备的占地面积最小的web服务器
- 将PHP/MMySQL转移到新的Web服务器,所有链接都会出现标签
- API的Web服务器
- 在drawImage、C#、AJAX、ASP.NET之后将图像从画布保存到Web服务器
- 如何通过C程序与web服务器对话
- 更新网页上Web服务器程序的实时输出
- Javascript在本地工作,不会在Web服务器上工作