3

I'm a newbie in jQuery and don't understand how in jQuery Ajax returns data. I have some simple function to get some data like below

[WebMethod(EnableSession = false)]
protected int SignIn()
{
 return 0;
}

and in my .aspx page I have this

$(document).ready(function () {
 $("#si").click(function 
 () {
 $.ajax({
 type: "POST",
 url: "SignIn.aspx/SignIn",
 contentType: "application/json",
 success: function (txt) {
 alert(txt);
 }
 });
 });
 });

but in alert I get the whole SignIn.aspx (all html tags and so on). how to alert the 0 which the SignIn() returns?thanks

asked Nov 11, 2011 at 12:40
1
  • the function SignIn is in .asmx webservice? Commented Nov 11, 2011 at 16:17

3 Answers 3

4

Make the SignIn method static and public and show alert with following code: alert(txt.d);

answered Nov 11, 2011 at 12:46

1 Comment

try to use alert(txt); Explanation of d property here: encosia.com/never-worry-about-asp-net-ajaxs-d-again
3

You are asking an ASPX file for data and I think that should be an ASMX.

Check out Dave Ward's post where I leaned about all this: http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

The simplest example I could make looks like this:

Add a Web Service (ASMX) containing

using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
 [WebMethod]
 public int Status(int input) {
 return input + 1;
 }
}

Then in your HTML do

<html>
<head>
 <title>Test</title>
 <script src="jquery-1.6.2.min.js" ></script>
</head>
<body>
<script>
 $(function () {
 $.ajax({
 url: 'WebService.asmx/Status',
 data: '{ input: 0 }',
 type: 'POST',
 dataType: 'json',
 contentType: 'application/json',
 success: function (data, status) {
 alert(data);
 alert(typeof data);
 }
 });
 });
</script>
</body>
</html>

In the ajax call the string defined in data is the input to the web method. Names must match. In the success callback, the first alert shows the value returned (input plus one) and the second alert shows that it is a number, not a string. Because the datatype is set to JSON, the web method returns JSON allowing the data to be typed correctly.

Hope this helps.

answered Nov 11, 2011 at 12:51

Comments

3

Try this sample. I have passed the id from aspx to handler and just returned from there to show the server side data to aspx again

this is sample example .....

Handler code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestProject
{
 /// <summary>
 /// Summary description for TestHandler1
 /// </summary>
 public class TestHandler1 : IHttpHandler
 {
 public void ProcessRequest(HttpContext context)
 {
 string id = context.Request["id"];
 context.Response.ContentType = "text/plain";
 context.Response.Write(id);
 }
 public bool IsReusable
 {
 get
 {
 return false;
 }
 }
 }
}

and in aspx

 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title></title>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
 </head>
<body>
 <form id="form1" runat="server">
 </form>
 <script type="text/javascript">
 $(function () {
 $.ajax({
 type: 'POST',
 url: 'TestHandler.ashx?id='+ 1,
 data: 'id=' + 1,
 success: function (msg) {
 alert(msg);
 }
 });
 });
 </script>
</body>
</html>
answered Nov 11, 2011 at 12:43

4 Comments

That will give back the number passed in as a string, but I suspect the OP would like JSON data returned, so the int is an int.
@nickd the op said "jQuery and don't understand how in jQuery Ajax returns data" .. I am explaining this as sample to that comment..
thanks, but I don't get it exactly, you used POST in ajax but used the url to send data? is the url only for detecting the method?
The response write is what will be returned... you could build the json and write it. if the json is valid, it can be used the same way...

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.