Answered by:
Call a method from server-side, but I can't get it

Question
-
User1572049755 posted
Hi all,
Thank you for your time. I am learning how to use Ajax to call a server method and return some value. But, my test code can't return a sting. Below is my code.
<script src="Scripts/jquery-2.1.3.js"></script> <script type="text/javascript"> $(document).ready( function () { $.ajax( { type: "POST", url: "AjaxCallWebMethod(string).aspx/ReturnString", data: "firstName=Swan&lastName=W", //contentType: "application/json; charset=utf-8", dataType: "text", success: function (data) { $("#Text1").html(data); } }) } ); </script> </head> <body> <form id="form1" runat="server"> <div> <input id="Text1" type="text" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> </form>
[WebMethod] public static string ReturnString(string firstName,string lastName) { string Text = "Hello"+firstName+lastName; return Text; }
So, kindly provide a solution and provide me a good tutorial about how to use Ajax. Besides call method from server-side, I also want to learn how to call WebAPI, WCF...
Please help me.
Swan
Sunday, May 3, 2015 11:04 PM
Answers
-
User1576270086 posted
Hi,
check bellow links
http://www.codeproject.com/Articles/349598/Introduction-to-using-jQuery-with-Web-Services
http://www.ezzylearning.com/tutorial/calling-asp-net-web-services-using-jquery-ajax
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 4, 2015 1:59 AM -
User61956409 posted
Hi SwanVW,
Thanks for your post.
I am learning how to use Ajax to call a server method and return some value. But, my test code can't return a sting.Please refer to the following sample to call a web method using jQuery AJAX.
$("#btncallwebmethod").click(function () { $.ajax({ type: "Post", url: "AjaxCallWebmethod.aspx/ReturnString", data: "{'firstName':'Swan','lastName':'W'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { var mes = data.d; alert(mes); }, error: function (err) { alert(err); } }); })
[WebMethod] public static string ReturnString(string firstName, string lastName) { string Text = "Hello" + firstName + lastName; return Text; }
For more information about jQuery AJAX, please refer to this link.
http://api.jquery.com/jquery.ajax/
Best Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 4, 2015 10:39 PM
All replies
-
User1576270086 posted
Hi,
check bellow links
http://www.codeproject.com/Articles/349598/Introduction-to-using-jQuery-with-Web-Services
http://www.ezzylearning.com/tutorial/calling-asp-net-web-services-using-jquery-ajax
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 4, 2015 1:59 AM -
User1572049755 posted
Hi A.Suresh,
Thank you for your help. The links you provided are helpful to me. But how about others' (WCF, WebAPI), I want to know how to call them by Ajax. Sorry, I am not sure if it is similar as calling web service.
However, the most important thing is above code. How to revise it? Besides get string value, I also want to return object, list, and other types' value.
Please help me.
Thanks,
Swan
Monday, May 4, 2015 2:26 AM -
User61956409 posted
Hi SwanVW,
Thanks for your post.
I am learning how to use Ajax to call a server method and return some value. But, my test code can't return a sting.Please refer to the following sample to call a web method using jQuery AJAX.
$("#btncallwebmethod").click(function () { $.ajax({ type: "Post", url: "AjaxCallWebmethod.aspx/ReturnString", data: "{'firstName':'Swan','lastName':'W'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { var mes = data.d; alert(mes); }, error: function (err) { alert(err); } }); })
[WebMethod] public static string ReturnString(string firstName, string lastName) { string Text = "Hello" + firstName + lastName; return Text; }
For more information about jQuery AJAX, please refer to this link.
http://api.jquery.com/jquery.ajax/
Best Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 4, 2015 10:39 PM -
User1572049755 posted
Hi Fei,
Thank you for your help.
Regards,
Swan
Wednesday, May 6, 2015 4:20 AM