See this post for a description of how this can be accomplished using AJAX for ASP.NET MVC.
If you have control of the service your self and if it is possible you could consider exposing it as a REST-like interface via WebHttpBinding and WCF. Then use jQuery to query the service.
WebService CODE :
-----------------
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class CommentService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public DateTime HelloWorld()
{
return DateTime.Now;
}
}
You should focus on below three points which I update in your code.
If HelloWorld method contain no parameter, then simply remove data attribute from $.ajax call
Remove local host number from url attribute just write the correct path of webservice + method.
Modify ScriptMethod like [ScriptMethod(ResponseFormat = ResponseFormat.Json)], because you need to select either JSON or XML in ResponseFormat.
Now update your code with my defined code and then let me know if any query still remains.
When you return DateTime.Now from your HelloWorld method. Format of datetime will be some time like this /Date(1297246301973)/
Modify onSuccess attribute like this,
success: function (msg) {
var date = msg.d;
var parsedDate = new Date(parseInt(date.substr(6)));
var newDate = new Date(parsedDate);
var getMonth = newDate.getMonth();
var getDay = newDate.getDay();
var getYear = newDate.getYear();
var standardDate = getMonth + '/' + getDay + '/' + getYear;
alert(standardDate);
},
takao200
None
0 Points
6 Posts
using jQuery.ajax to calling web service on asp.net MVC
Oct 30, 2012 08:33 PM|LINK
hi everybody.
it is not working on asp.net mvc. how can i make this work.
$.ajax({
url: "http://localhost:3129/CommentService.asmx/HelloWorld",
data: "{}",
type: "POST",
contentType: "application/json; charset=utf-8",
processData: false,
dataType: "json",
success: function (msg) {
alert(msg.d);
},
error: function (xhr,status,e) { alert(e.responseText); }
});
it is my webservice
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class CommentService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod]
public DateTime HelloWorld()
{
return DateTime.Now;
}
}
asteranup
All-Star
30184 Points
4906 Posts
Re: using jQuery.ajax to calling web service on asp.net MVC
Oct 31, 2012 03:20 AM|LINK
Hi,
Can you debug and check what is the output for-
xhr in error function.
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
takao200
None
0 Points
6 Posts
Re: using jQuery.ajax to calling web service on asp.net MVC
Nov 02, 2012 09:40 PM|LINK
xhr.responseText is 'it is HTTP Error 400 bad request.'
RameshRajend...
Star
7983 Points
2099 Posts
Re: using jQuery.ajax to calling web service on asp.net MVC
Nov 02, 2012 09:42 PM|LINK
See this post for a description of how this can be accomplished using AJAX for ASP.NET MVC.
If you have control of the service your self and if it is possible you could consider exposing it as a REST-like interface via WebHttpBinding and WCF. Then use jQuery to query the service.
takao200
None
0 Points
6 Posts
Re: using jQuery.ajax to calling web service on asp.net MVC
Nov 03, 2012 04:37 PM|LINK
come on guys.
is there anyone can does it work?. i'm hopeless
matifnadeem
Contributor
4972 Points
1158 Posts
Re: using jQuery.ajax to calling web service on asp.net MVC
Nov 13, 2012 06:43 AM|LINK
Hi takao200,
There is some thing wrong with your code. Modify your code like this
jQuery CODE : ------------- $.ajax({ url: "CommentService.asmx/HelloWorld", type: "POST", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { alert(msg.d); }, error: function (xhr,status,e) { alert(e.responseText); } });WebService CODE : ----------------- [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class CommentService : System.Web.Services.WebService { [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public DateTime HelloWorld() { return DateTime.Now; } }You should focus on below three points which I update in your code.
Now update your code with my defined code and then let me know if any query still remains.
Cheers
M Atif Nadeem
Mark as Answer if you got right thing
Read my blog | Follow me on LinkedIn
matifnadeem
Contributor
4972 Points
1158 Posts
Re: using jQuery.ajax to calling web service on asp.net MVC
Nov 13, 2012 06:52 AM|LINK
Hi again,
When you return DateTime.Now from your HelloWorld method. Format of datetime will be some time like this /Date(1297246301973)/
Modify onSuccess attribute like this,
success: function (msg) { var date = msg.d; var parsedDate = new Date(parseInt(date.substr(6))); var newDate = new Date(parsedDate); var getMonth = newDate.getMonth(); var getDay = newDate.getDay(); var getYear = newDate.getYear(); var standardDate = getMonth + '/' + getDay + '/' + getYear; alert(standardDate); },Read this article.
Cheers
M Atif Nadeem
Mark as Answer if you got right thing
Read my blog | Follow me on LinkedIn
takao200
None
0 Points
6 Posts
Re: using jQuery.ajax to calling web service on asp.net MVC
Nov 18, 2012 12:09 PM|LINK
hi Matif.
thanks for your reply. but it is not working. i tried what you said but there is something wrong about url path.
$.ajax({ url: "CommentService.asmx/HelloWorld",// i thing this is wrong type: "POST", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { alert(msg.d); }, error: function (xhr,status,e) { alert(e.responseText); } });matifnadeem
Contributor
4972 Points
1158 Posts
Re: using jQuery.ajax to calling web service on asp.net MVC
Nov 19, 2012 04:28 AM|LINK
Hi again,
Make sure that the path of webservice is correct. If it is on root then this line passes
and if not then set the correct path
M Atif Nadeem
Mark as Answer if you got right thing
Read my blog | Follow me on LinkedIn
takao200
None
0 Points
6 Posts
Re: using jQuery.ajax to calling web service on asp.net MVC
Nov 19, 2012 02:18 PM|LINK
the error is 'undefined'.i wanna send you the source code if you will look at.