0

I am just starting to mess around with Page Methods and jQuery together with not much success.

Below is my sample code...

Default.aspx.cs

[WebMethod]
public static string test()
{
     return "testing 123";
}

test.js

$(document).ready(function()
{
    $("#Result").click(function()
    {
        $.ajax({
            type: "POST",
            url: "Default.aspx/test",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg)
            {
                alert(msg);
            }
        });
    });
});

If I set a breakpoint at 'return "testing 123";' it never gets hit, also when I try doing http://localhost/default.aspx/test I get the whole page posted back, same with the jQuery function.

I have also tried using scriptmanager and MS AJAX PageMethods.test(); with the same exact result.

John Saunders
  • 160,644
  • 26
  • 247
  • 397

7 Answers7

2

I figured it out and it had nothing to do with JQuery or PageMethods... I have a URL Rewriter that's intercepting and redirecting and killing whatever is getting POSTed.

Thanks all for the help! -Goosey

1

just tried what you're doing with a separate file and everything works ok.

do you have a "scriptmanager" on your page? try removing it. Pagemethods/jQuery ajax work perfectly without it.

andryuha
  • 1,526
  • 3
  • 15
  • 31
0

I'm not sure if this is your problem, but try changing your type from a POST to a GET. If you're going to http://localhost/default.aspx/test in your browser and it's working then you know it's working for a GET operation because that's what your browser is doing.

Joseph
  • 25,330
  • 8
  • 76
  • 125
  • It's kind of strange, it seems both methods produce the same result. The $.ajax call will return with data, but it's the whole page. So if I did http://localhost/default.aspx/test in my browser it would return the whole default.aspx page content... but the same happens if i just do http://localhost/default.aspx/doesntexist123123 –  Sep 04 '09 at 18:58
  • @Goosey In that case I would suggest putting the webmethod inside an actual service (.asmx extension) See what that gets you. – Joseph Sep 04 '09 at 19:03
0

Theres some good info here. Also here.

Also, I don't know if it matters, but try making an actual web service page and putting the method in there, i.e. one with an .asmx extension.

Community
  • 1
  • 1
maxpower47
  • 1,666
  • 1
  • 10
  • 14
  • TY for the quick response. I did research both of those pages before I posted here. If I use a web service instead of PageMethods it does work, and I can get it to return in either XML or JSON. Just wanted to get the PageMethods Version working. –  Sep 04 '09 at 18:56
0

First, you have to check that your targeted elements (#result) are not refreshed using UpdatePanels, otherwise you can use the live functionality like that:

$("#Result").live("click", function() { 
  ...
});

Then, is your jquery code inside the same page as the Page Method ?

Sébastien Ros - MSFT
  • 5,091
  • 29
  • 31
  • the #Result div does exist. And using FireBug (net panel) I can see that it's POSTing and getting a response from the default.aspx/test url. It's just the entire page and not my return "test"; string. Also if I set a breakpoint there, it's never hit. –  Sep 04 '09 at 19:00
  • Sorry almost missed the 2nd half of your question. My JQuery code is inside a seperate .js file. the C# WebMethod is in code-behind. –  Sep 04 '09 at 19:03
0

I'm using the technique detailed here.

I set my data:

var contactData = "{'name':'" + txtName.val() + "',
                    'company':'" + txtCompany.val() + "',
                    'email':'" + txtEmail.val() + "'}";

Have my success method defined:

function success(result)
{
   alert(result.d);
}

Then call

`$.pageMethod("ContactUs/SendContactUsEmail", contactData, success);`

Job's a goodun.

Tristan Warner-Smith
  • 9,631
  • 6
  • 46
  • 75
0

This problem also occurs when you use PageMethods in combination with URL Routing. You can solve it by setting the url manually (in javascript):

PageMethods.set_path('/pagename.aspx'); 
w5l
  • 5,341
  • 1
  • 25
  • 43