Link to home
Start Free TrialLog in
Avatar of njgroup
njgroup

asked on

jquery ajax problem

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
Avatar of Eyal
Eyal
Flag of Israel image

you can't display master pages only aspx files

try this(change the page name)
http://localhost:6160/leamratech4/en/page.aspx/SaveEmail"
If its still not clear its likely this:

url: "MasterPageMain.master/SaveEmail",

Needs to be:

url: "PageName.aspx/SaveEmail",
Avatar of njgroup
njgroup

ASKER

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)
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
Avatar of njgroup

ASKER

how to deploy handler in jquery ajax?
ASKER CERTIFIED SOLUTION
Avatar of Member_2_4913559
Member_2_4913559
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of njgroup

ASKER

perfect, thanks very much