2

I've read many questions, forums, blogs, tried many things and I just can't seems to make it work.

I've tried using PageMethods.MyMethod() and that didn't work. Made sure my ScriptManager had EnablePageMethods ="true" and still nothing. I have a breakpoint on the server side and it never hits. Tried using ajax and still nothing

I'm 1st trying to understand how to make it work to then implemented on my program.

This is what I've tried so far:

Server-side:

[System.Web.Services.WebMethod]
    public static void SomeMethod(string subject, string body, string recipients, string CurrentUserId)
    {
        MessageBox.Show("In c#");
    }

JS:

function SomeFuntion()
{
        debugger; alert("Before web service");

        //PageMethods.CreateDraft(var1, var2, var3, var4);

        $.ajax
            (
                {
                    type: "POST",
                    url: "NewMessage.aspx/SomeMethod",
                    data: "{subject:'" + var1+ "', body:'" + var2+ "', recipients:'" + var3
                        + "', CurrentUserId:'" + var4+ "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success:
                        function()
                        {
                            alert("In ajax");
                        }
                }
            );
}

As you can see, I tried PageMethods and it didn't work. I know the function runs cause I see the alert message. By the way, the function is being called when the onclick event is fired of on a button. I don't get any errors and neither does it hit the break point on the MessageBox. This is new to me so any explanation would be very helpful too.

  • 3
    You call it `SomeMethod` on the server, but `CreateDraft` in the client. This *will* cause an issue. – Der Kommissar Apr 24 '15 at 00:25
  • To elaborate on what EBrown is saying, in your ajax url, you are invoking a method called 'CreateDraft'. The c# code you showed us has a method called 'SomeMethod'. So, your ajax call will complete without error which is why you are getting your ajax alert. But you until you change your url to invoke the correct method, your c# code will do nothing. – Walker Boh Apr 24 '15 at 02:44
  • not enough info to fully tell what is going on., but the above commenters are exactly right. Try changing the url line to: `url: "NewMessage.aspx/SomeMethod",` – DWright Apr 24 '15 at 02:52
  • would be good practice to implement an error callback (José, check the documentation http://api.jquery.com/jquery.ajax/ ) in the jquery ajax request. This will tell you if the url is referaing to a non-existing method. It could be that he just called called the server side method SomeMethod just to explain the architecture and that in reality his method it is really called CreateDraft – MFAL Apr 24 '15 at 11:31
  • Oops, lol. I forgot to change the url. That won't change anything cause in , my program, I have the right url, seems when I was pasting it here I misplaced the dummy with the real one. – José Corretjer-Gómez Apr 24 '15 at 12:51
  • A friend told me to had this library `[System.Web.Script.Services.ScriptService]`, to the server side, and not just webmethod. – José Corretjer-Gómez Apr 24 '15 at 13:01
  • @MFAL That would be helpful. then I could specify the problem – José Corretjer-Gómez Apr 24 '15 at 13:04
  • Take a look also at HttpHandler (*.ashx) in .Net. http://stackoverflow.com/questions/2948628/asp-net-passing-json-from-jquery-to-ashx – Jonathan Anctil Apr 24 '15 at 14:24

2 Answers2

0

Check for errors from the server:

function SomeFuntion()
{

    $.ajax({
            type: "POST",
            url: "NewMessage.aspx/CreateDraft",
            data: "{subject:'" + var1+ "', body:'" + var2+ "', recipients:'" + var3+ "', CurrentUserId:'" + var4+ "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend:function () 
            {
                 alert("about to send request");
            },
            success: function()
            {
                        alert("In ajax");
            },
            error: function(xhr, status, error) 
            {
                   var err = eval("(" + xhr.responseText + ")");
                   alert(err.Message);
            }
        );
}

more additions Since you are telling me that you are getting a "The HTTP verb POST used to access path '...' is not allowed" you might need configuration to enable .NET web service for http POST requests.

I have dropped .NET few years ago and therefore I have no direct experience but from what I could find in other sites you might need configuration at the server level such:

<system.web>
   <webServices>
     <protocols>
       <add name="HttpPost"/>
     </protocols>
   </webServices>
 </system.web>

Or directives above your method such

[ScriptMethod(UseHttpPost = true)]
public string SomeMethod()
{
    ....
}
MFAL
  • 1,090
  • 13
  • 19
  • His code is reaching the success function in his ajax call. There won't be any errors. His problem is that his ajax url is invoking a non-existent method – Walker Boh Apr 24 '15 at 02:44
  • Walker, he says that he gets the alert message, but he had an alert message before the ajax call. Of course you would assume that he meant the alert inside the success callback but since he says that he is new to this it is worth to implement the error callback as well. – MFAL Apr 24 '15 at 11:21
  • The alert that says "Before web service" runs but the messagebox doesn't and assuming that if that one doesn't run the alert with "in ajax" won't either. – José Corretjer-Gómez Apr 24 '15 at 12:54
  • This is the error msg `The HTTP verb POST used to access path '/NewMessage.aspx/SomeMethod' is not allowed` I found it a bit weird. – José Corretjer-Gómez Apr 24 '15 at 13:59
  • @José it seems to me a webserver configuration issue. Are you on a local IIS ? – MFAL Apr 24 '15 at 14:07
  • I had changed the webconfig and added httpPost and still can't, tried the scipt method but `UseHttpPost` is undefined. – José Corretjer-Gómez Apr 24 '15 at 14:27
0

I managed to figure it out with the help of a friend, I would post my answer but I believe it's quite long and complicated. I understood most of the things he did but I still didn't believe how it worked. It was pretty much almost the same thing I did but he made the JS in a separate file and set that file like a base JS. Called the web service there and from my JS he called that function that calls the web service. And it worked lol