简体   繁体   中英

Executing a C# method by calling it from client-side by ajax

I'm trying to execute a server side method using this technique:

Javascript Ajax function

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:

[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

There is no return in your method that is might be the poblem, you method should be like as below

[WebMethod]
 public static object storeLocal(string brand)
 {
     HttpContext.Current.Session.Add("Brand", brand);
     return "value" +brand;
 }

There are a couple of errors I can see with your ajax request:

  1. The url parameter value isn't a proper URL.
  2. Your not assigning your OnSuccess method correctly.

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:

 [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 eg

...
data: "brand=" + brand
...

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.

try to remove the href or put the onclick event inside a href='javascript:storelocal()' and return the value from webmethod.

keep a breakpoint in the webmethod and see if the content is getting passed to the webmethod or not.

The url and success doens't look good.

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.

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.

Try this to identify errors, this for seeing what is returned

error: function (error) {
    alert(JSON.stringify(error));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM