【发布时间】:2015-08-06 04:54:57
【问题描述】:
我有 1 个项目 webservice 和 1 个项目 web asp.net 我想通过 json(ajax) 插入数据 我通过后面的代码测试了文件服务,它很好, 代码js有错误 文件 WebService1:
public bool HelloWorld(student obj) {
SqlConnection cnn = new SqlConnection("Data Source=PHAMHOP-LAPTOP\\SQLEXPRESS;Initial Catalog=qlsv;Integrated Security=True");
cnn.Open();
SqlCommand cmd = new SqlCommand("insert into sinhvien(name,age) values(@name,@age)", cnn);
cmd.Parameters.AddWithValue("name", obj.name);
cmd.Parameters.AddWithValue("age", obj.age);
int row = cmd.ExecuteNonQuery();
if (row == 1){
return true;
} else {
return false;
}
}
文件aspx:
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function add() {
$.ajax({
type: "POST",
url: "http://localhost:51097/Service1.asmx/HelloWorld",
data: "{'id':'1' ,'name': 'Amit', 'age': '97'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert("ok");
}
});
}
</script>
<body>
<input type='Button' value='gui' OnClick='add()'/>
</body>
它不起作用。
【问题讨论】:
-
尝试删除
contentType: "application/json; charset=utf-8", dataType: "json", -
It does not work无济于事。发布您从浏览器控制台收到的实际错误消息。 -
它调用函数成功,但没有调用服务,兄弟@Singh
标签: javascript c# jquery asp.net ajax