0

how can i use ajax to call a server side method i tried this code but it gives me the alert error messsage and i can't find my problem please help and thank you :

enter code here

<%@ Control Language="C#" AutoEventWireup="true"   CodeFile="ImageEditor_UserControl.ascx.cs" Inherits="ImageEditor_UserControl" %>
<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
<script type ="text/javascript">
  $(document).ready(function () {
     $('#<%=uploadButton.ClientID %>').click(function () {

         $.ajax({
             type: "POST",
             url: "ImageEditor_UserControl.ascx/helo",
             data: "{}",
             contentType: "application/json;charset=utf-8",
             dataType: "json",
             async: true,
             cache: false,
             success: function () { alert("success"); },
             error: function () { alert("error"); }
         })
         return false;

     });
   });
    </script>

 <asp:Button ID="uploadButton" runat="server" Text="Upload"    />

C# Code

  [WebMethod]
   public static string helo() {

    return "Message from server.";
  }
bassem ala
  • 181
  • 1
  • 13
  • For ajax request ASP.NET normally offers UpdatePanel. If you want to upload image async way, try AsyncFileUpload from AjaxToolkit: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AsyncFileUpload/AsyncFileUpload.aspx – mshsayem Feb 20 '12 at 09:31
  • Are you sure the Url you are calling is correct? Try running it in debug mode, with a breakpoint on the method. See how far it gets. – IAmGroot Feb 20 '12 at 09:53
  • You should try changing your error function to report the error itself. See http://stackoverflow.com/questions/6792878/jquery-ajax-error-function but for example `error: function(jqXHR, textStatus, errorThrown) { alert(textStatus); } ` – Rawling Feb 20 '12 at 09:54

4 Answers4

1

You should call *.asmx files (there are other options but this is for the beginning).

Look out for tutorials on web services & ajax consuming.

Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
0

Have you checked on the line $('#<%=uploadButton.ClientID %>').click(function () { that the <%=uploadButton.ClientID %> is actually replace by the value and not taken literally?

Antoine
  • 5,055
  • 11
  • 54
  • 82
0

Do you use firefox? if yes, install the addon "FireBug". Enable firebug to check the request and the response.

Firebug will show you sometimes the error message returned from the server, as in you jquery syntax you are not loading the attributes for the method anonymous method callback for error.

error: function (req,error) { alert("error: " + req.statusText); }

This will give you a heads up on what is going wrong.

Dilberted
  • 1,172
  • 10
  • 23
-1

Unfortunately you cannot call a page method (server side method) that is a part of a user control. You will have to use a method in an aspx page.

Umair
  • 3,063
  • 1
  • 29
  • 50