<script type="text/javascript">
$(function () {
$("#submitBtu").bind("click", function () { submitData(); })
})
function submitData() {
var stuName = $("#stuName").val();
var stuPhone = $("#stuPhone").val();
if (stuName == "" || stuPhone == "") {
alert("请填写完整!");
return;
}
$.ajax({
type: "get",
url: "url" + new Date().getTime(),//api路径
data: { Name: stuName, Phonesdfd: stuPhone },
async: false,
cache: false,
dataType: ‘jsonp‘,
jsonp: "callbackfun", //传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback)
jsonpCallback: "jsonpCallback",
success: function (data) {
}
});
}
function jsonpCallback(data) //回调函数
{
alert(data.rtn + "," + data.message); //
}
</script>
<table>
<tr><td align="left">真实姓名:</td><td align="right"><input type="text" id="stuName" value="" /></td></tr>
<tr><td align="left">电话号码:</td><td align="right"><input type="text" id="stuPhone" value="" /></td></tr>
<tr><td align="center" colspan="2"><input type="button" value="确定" id="submitBtu"/></td></tr>
</table>
跨域代码:
public void Add(string Name, string Phonesdfd)
{
if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(Phonesdfd))
{
Response.Write("jsonpCallback({rtn:\"0\",message:\"真实姓名和电话号码为必填项!\"})");
return;
}
Regex dReg = new Regex(@"1[3|4|5|7|8]\d{9}$");//[0-9]{11,11}
if (!dReg.IsMatch(Phonesdfd.Trim()))
{
Response.Write("jsonpCallback({rtn:\"0\",message:\"手机号码有误,请重新填写!\"})");
return;
}
bool result = .....................................................................
string messageStr = "";
if (result)
{
messageStr = "jsonpCallback({rtn:\"1\",message:\"报名成功!\"})";
}
else
{
messageStr = "jsonpCallback({rtn:\"-1\",message:\"已经报名!\"})";
}
Response.Write(messageStr);
}