I'm trying to execute a server side method using this technique:
我正在尝试使用这种技术执行服务器端方法:
Javascript Ajax function
Javascript Ajax函数
function storeLocal(brand, type) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{brand:'" + brand + "'}",
url: "Jquery Site.Master/storeLocal",
datatype: "json",
success: OnSuccess(brand),
});
}
function OnSuccess(brand) {
alert(brand);
}
C# method:
c#方法:
[WebMethod]
public static object storeLocal(string brand)
{
HttpContext.Current.Session.Add("Brand", brand);
}
line of code to execute is:
执行的代码行为:
<li>
<a class="strong" onclick="storeLocal('petzl','harness')" href="About.aspx">Harnesses</a>
</li>
but its not executing correctly is there any particular error in my code? reasone i am using this method is because i want to have a dynamic menu for a small project and wish to store in session what specific "li" did a user select in session so that i can load the content in the redirected page. Thanks alot Adrian
但是它执行不正确我的代码中有什么特别的错误吗?我之所以使用这个方法,是因为我想要一个小项目的动态菜单,并希望在会话中存储用户在会话中选择的特定的“li”,以便在重定向页面中加载内容。谢谢艾德里安
4 个解决方案
#1
1
There is no return in your method that is might be the poblem, you method should be like as below
您的方法中没有返回可能是poblem,您的方法应该如下所示
[WebMethod]
public static object storeLocal(string brand)
{
HttpContext.Current.Session.Add("Brand", brand);
return "value" +brand;
}
#2
0
There are a couple of errors I can see with your ajax
request:
在您的ajax请求中,我可以看到一些错误:
- The
url
parameter value isn't a proper URL. - url参数值不是一个合适的url。
- Your not assigning your
OnSuccess
method correctly. - 您没有正确地分配OnSuccess方法。
Try changing it to:
试着改变它:
function storeLocal(brand, type) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{brand:'" + brand + "'}",
url: "ProperSiteUrl/storeLocal",
datatype: "json",
success: OnSuccess,
});
}
And you aren't returning anything from your storeLocal
web method. Try changing it to:
你不会从你的storeLocal web方法中返回任何东西。试着改变它:
[WebMethod]
public static object storeLocal(string brand)
{
HttpContext.Current.Session.Add("Brand", brand);
return ...;
}
Also, your sending JSON to the server, however, for a single parameter it you might find it easier just to send it as key/value pair e.g.
另外,您将JSON发送到服务器,但是,对于单个参数,您可能会发现将其作为键/值对发送更容易。
...
data: "brand=" + brand
...
#3
0
i am not sure if your code is right! you have given both href and onclick and the page might navigate to about.aspx even before the onclick ajax event is completed.
我不确定你的代码是否正确!您已经给出了href和onclick,页面可能会导航到about。aspx甚至在onclick ajax事件完成之前。
try to remove the href or put the onclick event inside a href='javascript:storelocal()' and return the value from webmethod.
尝试删除href或将onclick事件放在href='javascript:storelocal()'中,并从webmethod返回值。
keep a breakpoint in the webmethod and see if the content is getting passed to the webmethod or not.
在webmethod中保存一个断点,看看是否将内容传递给webmethod。
#4
0
The url and success doens't look good.
url和成功并不好看。
1 - Within the ajax call you don't pass a argument to the success function. it will be returned something by the webmethod specified in c#. you specify data in the data propriety, and that is used as the arguments passed to the webmethod.
1 -在ajax调用中,不向success函数传递参数。它将由c#中指定的webmethod返回。您可以在数据恰当性中指定数据,并将其用作传递给webmethod的参数。
2 - You can't call the webmethod using you master page, it has to be specified in the page that you are working. the aspx file not the master. The page inherits from master, but it's not the master. Is a page with specification of the master page file.
2 -你不能用你的主页调用webmethod,它必须在你正在工作的页面中指定。aspx文件不是主文件。这个页面继承了master,但它不是master。是具有主页面文件规范的页面。
Try this to identify errors, this for seeing what is returned
尝试此操作以识别错误,以查看返回的内容
error: function (error) {
alert(JSON.stringify(error));
}
#1
1
There is no return in your method that is might be the poblem, you method should be like as below
您的方法中没有返回可能是poblem,您的方法应该如下所示
[WebMethod]
public static object storeLocal(string brand)
{
HttpContext.Current.Session.Add("Brand", brand);
return "value" +brand;
}
#2
0
There are a couple of errors I can see with your ajax
request:
在您的ajax请求中,我可以看到一些错误:
- The
url
parameter value isn't a proper URL. - url参数值不是一个合适的url。
- Your not assigning your
OnSuccess
method correctly. - 您没有正确地分配OnSuccess方法。
Try changing it to:
试着改变它:
function storeLocal(brand, type) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{brand:'" + brand + "'}",
url: "ProperSiteUrl/storeLocal",
datatype: "json",
success: OnSuccess,
});
}
And you aren't returning anything from your storeLocal
web method. Try changing it to:
你不会从你的storeLocal web方法中返回任何东西。试着改变它:
[WebMethod]
public static object storeLocal(string brand)
{
HttpContext.Current.Session.Add("Brand", brand);
return ...;
}
Also, your sending JSON to the server, however, for a single parameter it you might find it easier just to send it as key/value pair e.g.
另外,您将JSON发送到服务器,但是,对于单个参数,您可能会发现将其作为键/值对发送更容易。
...
data: "brand=" + brand
...
#3
0
i am not sure if your code is right! you have given both href and onclick and the page might navigate to about.aspx even before the onclick ajax event is completed.
我不确定你的代码是否正确!您已经给出了href和onclick,页面可能会导航到about。aspx甚至在onclick ajax事件完成之前。
try to remove the href or put the onclick event inside a href='javascript:storelocal()' and return the value from webmethod.
尝试删除href或将onclick事件放在href='javascript:storelocal()'中,并从webmethod返回值。
keep a breakpoint in the webmethod and see if the content is getting passed to the webmethod or not.
在webmethod中保存一个断点,看看是否将内容传递给webmethod。
#4
0
The url and success doens't look good.
url和成功并不好看。
1 - Within the ajax call you don't pass a argument to the success function. it will be returned something by the webmethod specified in c#. you specify data in the data propriety, and that is used as the arguments passed to the webmethod.
1 -在ajax调用中,不向success函数传递参数。它将由c#中指定的webmethod返回。您可以在数据恰当性中指定数据,并将其用作传递给webmethod的参数。
2 - You can't call the webmethod using you master page, it has to be specified in the page that you are working. the aspx file not the master. The page inherits from master, but it's not the master. Is a page with specification of the master page file.
2 -你不能用你的主页调用webmethod,它必须在你正在工作的页面中指定。aspx文件不是主文件。这个页面继承了master,但它不是master。是具有主页面文件规范的页面。
Try this to identify errors, this for seeing what is returned
尝试此操作以识别错误,以查看返回的内容
error: function (error) {
alert(JSON.stringify(error));
}