How to call a webservice method from an html page [javascript] with out refresh the page
102,371
Solution 1
Use jQuery for performin POST or GET request from your html page like this :
function fun()
{
var data="hello";
$.get("http://localhost/ws/service.asmx/HelloWord", function(response) {
data = response;
}).error(function(){
alert("Sorry could not proceed");
});
return data;
}
OR :
function fun()
{
var data="hello";
$.post('http://localhost/ws/service.asmx/HelloWord',{},function(response)
{ data = response;
}).error(function(){
alert("Sorry could not proceed");
});
return data;
}
Solution 2
You can send ajax request to a webservice
$.ajax({
url: "WebServiceURL",
data: "", //ur data to be sent to server
contentType: "application/json; charset=utf-8",
type: "GET",
success: function (data) {
alert(data);
},
error: function (x, y, z) {
alert(x.responseText +" " +x.status);
}
});
Author by
ocp
Updated on July 18, 2022Comments
-
ocp almost 2 years
I have a
webservice
which will return a value. Here my requirement is, I need to call thatwebservice
from anindex.html
page, that page have an html submit button. On that button click I am calling aJavaScript.
From there I want to call the web method. how can I achieve this.My webservice is
"localhost/ws/service.asmx";
and web method isHelloWorld
<input type="button" value="Submit" id="btn_submit" onclick ="return fun()"> function fun() { // here I want to call the "helloworld" method. return true; }
-
ocp over 10 yearsHi Aditya , thank you!. Here My webservice is "localhost/ws/service.asmx" and my webmethod is HelloWorld. So how can I give that function fun() { $.get("localhost/ws/service.asmx", function(Helloworld) { //alert(response); }); return true } is it ????
-
ocp over 10 yearssorry to say.. it is not .. first one is not throwing any error but it is not returning the output string. second one says undefined object data_value
-
ocp over 10 yearsThank you for your reply , but Sorry it is again returning "Undefind"
-
ocp over 10 yearsthe webservice is returning a string "Hello world".
-
Aditya over 10 yearsalert(response) and check what's being returned to your page?
-
ocp over 10 years'response' is undefined
-
Aditya over 10 yearsThen you are not returning anything from server side ? check once if you are using
context.Response.Write("Hello World");
to send response.. -
ocp over 10 years[WebMethod] public string HelloWorld() { return "Hello World"; } this is what my method.. simple one
-
Aditya over 10 years
public void HelloWord(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write("Hello world"); }
Try this -
ocp over 10 yearsI think the webservice call itself is not happening , I was trying to create the log file over the webservice call . but it is not generating any files.
-
ocp over 10 yearsthat too giving me undefined
-
Aditya over 10 yearsCheck the updated answer, if there is an error in the request
-
ocp over 10 yearsit is returning "Sorry could not proceed"
-
Aditya over 10 years
onclick ="fun();"
, try this -
ocp over 10 yearssame result "Sorry could not proceed"
-
ocp over 10 yearsI think the webservice call itself is not happening , I was trying to create the log file over the webservice call . but it is not generating any files
-
ocp over 10 yearsif am directly accessing this , it is giving the error "Request format is unrecognized for URL unexpectedly ending in '/HelloWord'."
-
ocp over 10 yearsbut if I am accessing the localhost/ws/service.asmx service and then after invoking the method from the browser it is working fine.
-
Aditya over 10 yearsTry setting the URL to your method as the url for GET request, simple
-
ocp over 10 yearssorry i dint get that
-
Aditya over 10 yearsThe URL that u access your method from your browser, set that url for the GET Request?
-
Andrew over 6 yearsYour functions will always return "hello", as they end instantly and before the callback functions are executed.