1

i have to call one C# function from java script.For that i am using ajax post request for calling C# function.But My C# function is not actually called from ajax script.I dont know what is the reason for not calling C# function?

This is my Ajax Code:

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>
      <script type="text/javascript">

      $(function () {
          $("#btnSend").click(function () {
              var image = document.getElementById("myCanvas").toDataURL("image/png");
              image = image.replace('data:image/png;base64,', '');

              $.ajax({
                  type: 'POST',
                  url: 'Default.aspx/UploadImage',
                  data: '{ "imageData" : "' + image + '" }',
                  contentType: 'application/json; charset=utf-8',
                  dataType: 'json',
                  success: function (msg) {
                      alert('Image sent!');                      
                  }
              });
          });
      });
    </script>

And this is my C# function :

namespace sample
{
    [ScriptService]
    public partial class _Default : System.Web.UI.Page
    {
        [WebMethod()]
        public static void UploadImage(string imageData)
        {

            FileStream fs = new FileStream("D:\\vs-2010projects\\delete_sample\\delete_sample\\myimages\\image.png", FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);

            byte[] data = Convert.FromBase64String(imageData);

            bw.Write(data);
            bw.Close();
        }
     }
  }
Saravanan
  • 11,372
  • 43
  • 143
  • 213
  • 2
    What does the web console say? – Grant Thomas May 29 '13 at 09:16
  • The Web console(Chrome) says POST http://localhost/delete_sample/Default.aspx/UploadImage 500 (Internal Server Error) jquery-1.6.1.min.js:18 send jquery-1.6.1.min.js:18 f.extend.ajax jquery-1.6.1.min.js:18 (anonymous function) Default.aspx:25 f.event.handle jquery-1.6.1.min.js:17 i.handle.k jquery-1.6.1.min.js:16 – Saravanan May 29 '13 at 09:20
  • Well there you go, something is wrong with the function it seems. You debugging, have a breakpoint set? – Grant Thomas May 29 '13 at 09:22
  • Is there some problem with my C# function? – Saravanan May 29 '13 at 09:22
  • I'm can't be certain but this looks suspicious: `data: '{ "imageData" : "' + image + '" }'`. It should probably be `data: { imageData: image }` – Candide May 29 '13 at 09:22
  • I am also tried with like you said but the same error showing... – Saravanan May 29 '13 at 09:25
  • You are trying to upload an image via ajax? – Phong Vo May 29 '13 at 09:32
  • yes...i have to upload a image on canvas to server via ajax – Saravanan May 29 '13 at 09:40
  • 1
    what does control shift j show on firefox and what is toDataURL ? – tariq May 29 '13 at 09:44
  • @tariq: in firefox it doesn't show any error.But one warnings on my code... – Saravanan May 29 '13 at 09:48
  • In cases like this a 500 error often means malformed jSON. Yours does look correct at first glance. Second best guess would be an exception related to filesystem access. I suggest you debug in VS and, per Grant, set a breakpoint. Also it might help to have Fiddler running so you can inspect call and response, including JSON decoding, more easily. – Stephen Kennedy May 29 '13 at 10:12
  • I'm not familiar with toDataURL. Could you run the program called Fiddler and paste in the request made to the server by your web method call, including the payload? – Stephen Kennedy May 29 '13 at 10:17
  • @StephenKennedy : thank you very much... in fiddler, i looked inspectors section and also stacktrace of the exception...[ArgumentException: Unknown web method UploadImage. Parameter name: methodName] System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) +537330 System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) +213 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) – Saravanan May 29 '13 at 10:38
  • @Stephen Kennedy: can you help me with this... – Saravanan May 29 '13 at 10:38
  • It looks like it can't find the web method called UploadImage. Does your .aspx page reference the .aspx.cs page? Are you calling the correct URL? – Stephen Kennedy May 29 '13 at 10:40
  • <%@ Page Language="C#" Inherits="_Default" CodeFile="Default.aspx.cs" %> should be at the top of the .aspx page. – Stephen Kennedy May 29 '13 at 10:41
  • my Default.aspx page contains the web method UploadImage in Default.aspx.cs file – Saravanan May 29 '13 at 10:43
  • The error is pretty clear "ArgumentException: Unknown web method UploadImage. Parameter name: methodName" so check that the Default.aspx.cs page is referenced by a Page attribute at the top of the Default.aspx page. You've correctly labelled the page method with a WebMethod attribute and made it static so I can't think of why else it can't be found. – Stephen Kennedy May 29 '13 at 10:54
  • @StephenKennedy : am very sorry. Just now i checked it aspx page reference problem.Now i rectified it. but another exception is shown in the fiddler... "Message":"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. "ExceptionType":"System.ArgumentException"} – Saravanan May 29 '13 at 11:49
  • OK as that fixed it I've posted as an answer. Please accept as the correct answer. The new issue is easily solvable and almost certainly already answered on StackOverflow. Please search for the answer and if you can't find one... well, I'm not sure if you should post another question or ask on this page. – Stephen Kennedy May 29 '13 at 12:05
  • http://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config may help with the maxJsonLength issue. If not there's plenty of info on Google/SO. – Stephen Kennedy Jun 08 '13 at 13:52

1 Answers1

1

The error was [ArgumentException: Unknown web method UploadImage. Parameter name: methodName].

<%@ Page Language="C#" Inherits="_Default" CodeFile="Default.aspx.cs" %> should be at the top of the .aspx page as the compiler isn't aware of your code behind without it.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108