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.
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.
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; }
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.
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.
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);
}
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With