webservice小问题
TestForm.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestForm.aspx.cs" Inherits="wt.TestForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jss/jquery.json-2.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
function clickone() {
var par = { str: "大家好!!!"};
var json_str = $.toJSON(par);
$.ajax({
url: "TestData.asmx/HelloWorld",
type:"POST",
contentType:"application/json;charset=utf-8",
dataType: "json",
data:json_str,
success: function (result) {
alret(result.d);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="clickone()" value="one"/>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace wt
{
/// <summary>
/// TestData 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class TestData : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(string str)
{
return "Hello World" + str;
}
}
}
function clickone() {
alert('检查是否执行了这里?');
var par = { str: "大家好!!!"};
var json_str = $.toJSON(par);
$.ajax({
url: "TestData.asmx/HelloWorld",
type:"POST",
contentType:"application/json;charset=utf-8",
dataType: "json",
data:json_str,
success: function (result) {
alret(result.d);
}
});
}