We help IT Professionals succeed at work.

jquery ajax problem

njgroup
njgroup asked
on
hi,

in asp.net 4.0 c#,

I have master page and I need to call ajax function to get some data from server, so I have this jquery function:

 
$("#save_email_db").live({
                    click: function (event) {
                        event.preventDefault();
                        var email = $('#email_newsletter').val();
                        if (IsValidEmail(email)) {
                            $.ajax({
                                type: "POST",
                                url: "MasterPageMain.master/SaveEmail",
                                data: email,
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                timeout: 7000,
                                success: function (msg) {
                                    var res = msg.d;
                                    alert(res);
                                    if (res == "Added Successfully!") {
                                    }
                                    else {
                                    }
                                },
                                error: function (xhr, status, errorThrown) {
                                }
                            });

                        }
                    }
                });

Open in new window


now, in master page behind code (C# code) I have this function in to be called by ajax:

 
[System.Web.Services.WebMethod]
    public static string SaveEmail(string name)
    {
        try
        {
            return "Added Successfully!";
        }
        catch (Exception ex)
        {
            return "DB Error, Contact us Please!";
        }
    }

Open in new window


now when I call run the code, I'm getting this error:

"NetworkError: 403 Forbidden - http://localhost:6160/leamratech4/en/MasterPageMain.master/SaveEmail"

so what is the problem, and how can I solve it
Comment
Watch Question

Commented:
you can't display master pages only aspx files

try this(change the page name)
http://localhost:6160/leamratech4/en/page.aspx/SaveEmail"

Commented:
If its still not clear its likely this:

url: "MasterPageMain.master/SaveEmail",

Needs to be:

url: "PageName.aspx/SaveEmail",

Author

Commented:
hi,

do u mean I have to put the c# code in aspx page and call the page.aspx in the ajax function?

if so, I will get a big problem, because I have many aspx pages relying on this master page (its not only one aspx page)

Commented:
I can think on 2 options you can follow

1) use handler (best solution, avoids all the pipeline)
2) inherit all the pages from your own custom class that inherits from page

Author

Commented:
how to deploy handler in jquery ajax?
Commented:
Sorry. I didnt realize the actual method itself was on the masterpage.

Well you are trying to call a "Page Method" from jquery ajax. If the method is actually on your masterpage then you could put the "Page Method" on a specific page and then call that page from ajax.

I can see why you may not want to do this because you want the "Page Method" available for any page placed in the master.

You could write a webhandler that would be fine. You could override the page class. I think this would cause you a variation of the same issue as putting the "Page Method" on a page, lots of page specific changes(or if put on the masterpage... unreachable).

Best solution is to use a "WebMethod" instead of a "PageMethod" in this case.

I notice in a lot of articles people use these terms interchangably. I think its because they use the same [WebMethod] decorator.

Anyway I'm refering to using a [WebMethod] in a webservice that would be available to any of your pages. It sets up almost identically to the "Page Method" you are using and is called the same in jquery, its just in a class of its own so its available to everyone.

This article will show you all the common ways to use ajax with jquery and asp.net (pagemethod, webhandler, webmethod).

http://www.mikesdotnetting.com/Article/104/Many-ways-to-communicate-with-your-database-using-jQuery-AJAX-and-ASP.NET

I think the choice for you is webhandler or webservice/webmethod.





Author

Commented:
perfect, thanks very much