我想給我的函數「InsertMethod」使用Ajax調用,但函數沒有被調用。 但是,如果我在新頁面中使用與下面相同的代碼,那麼它的工作即調用函數使用Ajax。任何人都可以請幫我解決這個問題。 我還有一個查詢。我無法訪問網頁的任何控制在Web方法「Insertmethod」Ajax調用我的aspx頁
下面是腳本
<script type="text/javascript">
function getDistinctChains() {
$.ajax({
url: 'Tax_Type_Master.aspx/InsertMethod',
type: 'POST',
contentType: "application/json; charset=utf-8",
data: "{'Name':'" + TaxType.value + "'}",
//async: false,
success: function(response) {
$('[id*=txtTaxtypeName]').val('');
alert("Record Has been Saved in Database");
},
error: function() { //console.log('there is some error');
alert('there is some error');
}
});
}
$(document).ready(function() {
$('[id*=ImageSaveFooter]').click(function() {
getDistinctChains();
});
});
</script>
下面
是我的CS代碼
public partial class Tax_Type_Master : System.Web.UI.Page
{
Generic gn = new Generic();
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
Master.FillHeader("Tax Type Master");
BindGrid();
}
}
catch (Exception ex)
{
Master.ErrorMessage(ex.Message);
}
}
}
[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat =
System.Web.Script.Services.ResponseFormat.Json,
UseHttpGet = false)]
public static string InsertMethod(string Name)
{
TextBox TaxTypeName = (TextBox)grd_Master.FooterRow.FindControl("txtTaxtypeName");
int count = (int)gn.ExecuteScaler("Check_TaxType_Ref_TaxTypeMaster", TaxTypeName.Text.ToUpper().Trim());
if (count == 0)
{
gn.ExecuteNonQuery("Insert_Taxtype_Master",
TaxTypeName.Text.ToUpper().Trim());
BindGrid();
Master.SuccessMessage("Record Inserted Successfully..");
}
else
{
Master.ValidationMessage("Record Already Exist..");
}
return "True";
}
你粘貼的代碼是否正確? document.ready關閉括號});在腳本結束標記之後。 – Sandeep
複製代碼時發生錯誤。我已更正代碼 – poonam
檢查網址,請嘗試使用'/ Tax_Type_Master.aspx/InsertMethod' –