我想使用Web服务将数据插入数据库中,但是AJAX没有调用Web服务。
HTML代码
<form id="form1">
<div> <input type="text" name="name2" runat="server" id="Text12" placeholder="Your Name" />
<input type="button" value="Save Data" onclick="SendD()" id="send1" />
</div>
</form>
AJAX编码:
function SendD() {
var name = document.getElementById('Text12').value;
alert();
$.ajax({
type: "POST",
url: "Web2.asmx/Data2",
data: "{Namen: '" + name + "'}", // here we are specifing the data as a JSON object, not a string in JSON format
// this will be converted into a form encoded format by jQuery
// even though data is a JSON object, jQuery will convert it to "firstName=Aidy&lastName=F" so it *is* form encoded
contentType: "application/json; charset=utf-8", // we are sending in JSON format so we need to specify this
dataType: "json", // the data type we want back. The data will come back in JSON format
async: false,
success: function (data) {
alert("Succes");
},
failure: function (data) {
alert("F");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
}
});
}
Web服务代码运行正常
[WebMethod]
public static string Data2(String Namen)
{
string json = "";
string connstr = ConfigurationManager.ConnectionStrings["DBL"].ConnectionString;
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
//insert into Claim_Status_Trans(Claim_ID,Claim_Status,Status,Deleted_Flag,Row_upd_date)values(@claimid,7,1,0,@rowupddate)
string qry1 = "insert into SendDataTable(Name)values('" + Namen + "')";
SqlCommand movestoclaims = new SqlCommand(qry1, conn);
movestoclaims.ExecuteNonQuery();
conn.Close();
return json;
}
预先感谢
我尝试检查警报功能按钮是否只能在不调用Web服务的ajax上正常工作。