Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call C# webservices from javascript and consume it (json format) [closed]

I have created a c# webservice and i am trying to call it and consume it from a javascript script, what is the way or the best way to do it, thanks in advance. I'll explain more: this is the web service:

 public class DocumentInfo : System.Web.Services.WebService
{

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    public string GetDocumentInfo(string id)
    {
        Document document = new Document(Int32.Parse(id));    
        string output = JsonConvert.SerializeObject(document);
        return output;
    }
}

I have tested it, it works, when i tried the suggested ajax solutions, i got this error 500 Internal Server Error.

like image 756
Vervatovskis Avatar asked Sep 06 '12 08:09

Vervatovskis


People also ask

What is call in C?

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

What is function call in C with example?

Actual parameters: The parameters that appear in function calls. Formal parameters: The parameters that appear in function declarations. For example: #include <stdio.h> int sum(int a, int b) { int c=a+b; return c; } int main( { int var1 =10; int var2 = 20; int var3 = sum(var1, var2); printf("%d", var3); return 0; }

Is C call by value?

Call by value in C In call by value method, the value of the actual parameters is copied into the formal parameters. In other words, we can say that the value of the variable is used in the function call in the call by value method.

What is call reference?

Whenever calling a function, rather than passing the variables' values, we pass its address instead (location of variables) to the function. Thus, it has its name as Call by Reference.


2 Answers

Read some of the tutorials

http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/ http://weblogs.asp.net/jalpeshpvadgama/archive/2010/08/29/calling-an-asp-net-web-service-from-jquery.aspx

function TestService() 
{              
    try 
      {

       $.ajax({
         type: "POST",
         url: "http://webserviceURL.asmx/YourWebMethodName",
         data: "{'abc':'" + 123 + "'}", // if ur method take parameters
         contentType: "application/json; charset=utf-8",
         success: SuccessTestService,
         dataType: "json",
         failure: ajaxCallFailed
      });
    }
    catch (e)
    {
        alert('failed to call web service. Error: ' + e);
    }
}

function SuccessTestService(responce) {
    alert(eval(responce.d));
}


function ajaxCallFailed(error) {
        alert('error: ' + error);

    }
like image 142
Talha Avatar answered Oct 31 '22 16:10

Talha


You need to make an AJAX request and wait for the callback to receive the data.

A very simple example using jQuery:

$.ajax({
  url: "/my_service.cs"
}).done(function(data) { 
  console.log("Received: ", data);
});
like image 38
alexandernst Avatar answered Oct 31 '22 15:10

alexandernst