[英]Getting error on passing argument to the code behind method using jquery ajax
I don't know what's wrong with this code. 我不知道这段代码有什么问题。 I am not able to implement it properly.
我无法正确实施。 Thanks in advance for help.
在此先感谢您的帮助。 This is what I've tried so far and stuck
到目前为止,这是我尝试过的并遇到的问题
<script type="text/javascript">
$(document).ready(function () {
for (var key in localStorage) {
GetQuickVL(key);
}
});
function GetQuickVL(key) {
if (key.substring(0, 4) == "vhs-") {
$.ajax({
type: "POST",
url: "/QuickViewList.aspx/GetQuickVD",
data: '{key: ' +'1' + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.response);
},
error: function (response) {
alert(response.error);
}
});
}
}
function OnSuccess(response) {
alert('df');
}
</script>
and the C-Sharp code is C-Sharp代码是
[WebMethod]
public int GetQuickVD(int key)
{
return key;
}
key
is an int
. key
是一个int
。 You've passed it as a string
: 您已将其作为
string
传递:
'{key: ' +'1' + '}',
Either do: 要么做:
{key: 1 },
Or make your web method take an object
or string
as its parameter: 或让您的网络方法将
object
或string
作为其参数:
[WebMethod]
public int GetQuickVD(object key)
{
return Convert.ToInt32(key);
}
Here is a complete working sample (I just tested this). 这是一个完整的工作示例(我刚刚对此进行了测试)。 Adapt to suit your needs:
适应您的需求:
WebService: 网络服务:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService] // <- ** MAKE SURE THIS IS UNCOMMENTED!
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public int GetQuickVD(int key)
{
return key;
}
}
Aspx page: Aspx页面:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.foo').click(function () {
$.ajax({
type: "POST",
url: "WebService1.asmx/GetQuickVD",
data: '{key: ' + '1' + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("success");
}
});
})
});
</script>
<a href="#" class="foo">click me</a>
if the method is in your code behind (.aspx.cs file) try declaring the GetQuickVD method as static. 如果该方法在您代码的后面(.aspx.cs文件),请尝试将GetQuickVD方法声明为静态方法。 so it's public static int GetQuickVD(int key).
因此它是公共静态int GetQuickVD(int key)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.