views:

493

answers:

6

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.

A: 

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
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
@Goosey In that case I would suggest putting the webmethod inside an actual service (.asmx extension) See what that gets you.
Joseph
A: 

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.

maxpower47
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.
A: 

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
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.
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.
+1  A: 

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
A: 

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.

TreeUK
+2  A: 

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