在 IE9 中,以下代码显示 JScript 不支持 $.ajax() 方法,而在 FF 中,它没有显示任何错误,但它应该执行的任务没有完成。我还附上了我的 aspx 代码和方法后面的代码。我有在代码隐藏中包含所有必要的库。
<table class="style3">
<tr>
<td><label id="lname" >Name</td>
<td><input id="name" type="text" runat="server"/></td>
</tr>
<tr>
<td><label id="ldept">Department</label></td>
<td><select id="dept">
<option value="TnH">Travel & Hospitality</option>
<option value="BFS">Banking & Financial Services</option>
<option value="Insurance">Insurance</option>
<option value="Communication">Communication</option>
<option value="HealthCare">HealthCare</option>
</select>
</td>
</tr>
<tr>
<td><label id="lcontact">Contact</label></td>
<td><input id="contact" type="text" runat="server"/></td>
</tr>
<tr>
<td><label id="lage">Age</label></td>
<td><input id="age" type="text" runat="server"/></td>
</tr>
</table>
</div>
<asp:Button ID="hdnBtn" runat="server" onclick="AddEmployee" />
<asp:HiddenField ID="hname" runat="server" />
<asp:HiddenField ID="hdept" runat="server" />
<asp:HiddenField ID="hcontact" runat="server" />
<asp:HiddenField ID="hage" runat="server" />
</form>
<script type="text/javascript">
$(document).ready(function()
{
$("#dialog-form").parent().appendTo($("form:first"));
var hdnBtn=document.getElementById("<%=hdnBtn.ClientID %>");
$("#hdnBtn").hide();
$("#diaglog-form").hide();
$("#addBtn").click(function()
{
$("#diaglog-form").show();
$("#diaglog-form").dialog(
{
open:function()
{
$("#this").parent().appendTo($("form:first"));
},
title: 'Add New Employee',
modal:true,
minHeight:300,
minWidth:440,
buttons:
{
Add:function()
{
// $("#hname").val($("#name").val());
// $("#hdept").val($("#dept").val());
// $("#hcontact").val($("#contact").val());
// $("#hage").val($("#age").val());
// $("#hdnBtn").click();
var name=name.val();
var dept=dept.val();
var contact=contact.val();
var age=age.val();
$.ajax({
type: "POST",
url: "Default.aspx/GetData",
// data: "{name: '" + name.val() + "',dept:"+dept.val()+"',contact:"+contact.val()+"',age:"+age.val()+"'}",
data: {name:name,dept:dept,contact:contact,age:age},
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
processData:false,
success : function(data)
{
// in case you would use a return value in your webmethod
$("#btnAllEmp").click();
$(this).dialog("close");
}
});
},
close:function()
{
$(this).dialog("close");
}
}
}
);
});
});
</script>
我附上的图书馆:-
<script src="JQuery/jquery-1.9.0.min.js" type="text/javascript" />
<script src="JQuery/jquery.ui.dialog.js" type="text/javascript"></script>
<script src="JQuery/jquery-1.9.1.js" type="text/javascript"></script>
<script src="JQuery/jquery.ui.core.js" type="text/javascript"></script>
<script src="JQuery/jquery.ui.widget.js" type="text/javascript"></script>
<script src="JQuery/jquery.ui.mouse.js" type="text/javascript"></script>
<script src="JQuery/jquery.ui.button.js" type="text/javascript"></script>
<script src="JQuery/jquery.ui.draggable.js" type="text/javascript"></script>
<script src="JQuery/jquery.ui.position.js" type="text/javascript"></script>
<script src="JQuery/jquery.ui.resizable.js" type="text/javascript"></script>
<script src="JQuery/jquery.ui.button.js" type="text/javascript"></script>
<script src="JQuery/jquery.ui.dialog.js" type="text/javascript"></script>
<script src="JQuery/jquery.ui.effect.js" type="text/javascript"></script>
<link href="Stylesheets/jquery.ui.dialog.css" rel="stylesheet" type="text/css" />
<link href="Stylesheets/jquery.ui.all.css" rel="stylesheet" type="text/css" />
<link href="Stylesheets/jquery-ui.css" rel="stylesheet" type="text/css" />
代码隐藏方法:-
[WebMethod]
public static void GetData(string jsonData)
{
//string name = collection["name"] as string;
//string dept = collection["dept"] as string; ;
//string contact = collection["contact"] as string; ;
//string age = collection["age"] as string; ;
EmployeeEntity emp = new EmployeeEntity();
JavaScriptSerializer serializer = new JavaScriptSerializer();
emp = serializer.Deserialize<EmployeeEntity>(jsonData);
string name = emp.EmpName;
string dept = emp.EmpDept;
string contact = emp.EmpContact;
string age = emp.EmpAge;
string text = name + " " + dept + " " + contact + " " + age + " ";
System.IO.File.WriteAllText(@"D:\WriteText.txt", text);
SqlConnection connection = new SqlConnection(conString);
String cmdText = "INSERT INTO EMPLOYEE VALUES('" + name + "','" + dept + "','" + contact + "','" + age + "')";
SqlCommand command = new SqlCommand(cmdText, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
[编辑]
var name=$("#name").val();
var dept=$("#dept").val();
var contact=$("#contact").val();
var age=$("#age").val();
var data=JSON.stringify({name:name,dept:dept,contact:contact,age:age});
$.ajax({
type: "POST",
url: "Default.aspx/GetData",
// data: "{name: '" + name.val() + "',dept:"+dept.val()+"',contact:"+contact.val()+"',age:"+age.val()+"'}",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
processData:false,
success : function(data)
{
// in case you would use a return value in your webmethod
$("#btnAllEmp").click();
$(this).dialog("close");
}
});
并且在使用 JQuery 1.10.2 之后 $.ajax() 得到了认可。谢谢但是 GetData() 没有做任何事情,数据没有被插入到数据库中。请帮助
[编辑 1]
error:function(resp,textStatus,errorThrown)
{
alert("Error : "+errorThrown);
}
});
它给出“错误:内部服务器错误”。现在请帮助我