我在後面的代碼中定義了一個webMethod,如下所示。使用參數作爲本地變量的Ajax調用不起作用
[System.Web.Services.WebMethod]
public static string testCall(int qid, String answerContent)
{
log.Debug("this is call from jquery" + qid.ToString() + answerContent);
return "true";
}
我想通過調用jQuery中的ajax調用此方法如下。
<script>
$(".submitBtn").click(function (e) {
alert(this.id);
var qID = this.id;
$.ajax({
type: "POST",
url: '/default.aspx/testCall',
data: '{ "qid":' + qID + ', "answerContent":"test" }',
contentType: "application/json; charset=utf-8",
success: function() {
},
failure: function (response) {
alert("fail");
},
dataType: 'html'
});
});
</script>
但是這不起作用。
但是,我通過硬編碼參數值如下,它工作正常。
data: '{ "qid":"1234", "answerContent":"test" }'
但隨着var qID
傳遞作爲參數不工作
用'data:JSON.stringify({「qid」:qID,「answerContent」:「test」})測試一次' – Satpal
我假設您正在使用Webforms嗎? – Valkyrie
@Valkyriee是的。 –