我尝试开发一个简单的登录表单,在 asmx 文件中将按钮发布表单数据提交给我的方法。
问题是,当我尝试从表单标签内发布时,我会收到错误消息。
发布外部表单标签效果很好。
我提供了一个简化的问题示例:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#submitOutsideForm").click(function () {
AjaxPost();
});
$("#submitInsideForm").click(function () {
AjaxPost();
});
});
function AjaxPost() {
try {
$.ajax({
type: "POST",
url: "dummyWS.asmx/HelloToYou",
data: "{'name': '" + $('#name').val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
AjaxSucceeded(msg);
},
error: AjaxFailed
});
}
catch (e) {
alert("Exception in ajaxCall2Func: " + e.name + ".\n Error messege: " + e.message);
}
}
function AjaxSucceeded(result) {
alert(result.d);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
</script>
</head>
<body>
Enter your name:
<input id="name" />
<form id="form1" runat="server" style="border:1px solid green;">
<div>
<label for="email">some other form controls</label>
</div>
<input id="submitInsideForm" type="submit" value="Submit Inside Form" />
</form>
<input id="submitOutsideForm" type="submit" value="Submit Outside Form" />
</body>
</html>
在服务器端,我有这个简单的虚拟 WebService
<%@ WebService Language="C#" CodeBehind="~/App_Code/dummyWS.cs" Class="dummyWS" %>
…以及后面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class dummyWS : System.Web.Services.WebService {
[WebMethod()]
public string HelloToYou(string name)
{
return "Hello " + name;
}
}