0

I try to use ajax call in my aspx page. Here is my script:

<head runat="server">
<title></title>
<script type="text/javascript" src="jquery/ui/jquery-ui-1.8.23.custom.js"></script>

<script type="text/javascript" language="javascript">

    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "WebForm1.aspx/List",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () {
                alert('asd');
            }
        });

    });

</script>
</head>

Here is my server side code:

 [WebMethod]
 public static string[] List()
 {
      ...
 }

I put a break point List's first row but nothing happen. Do you have any suggestion, where I make a mistake?

Oded
  • 489,969
  • 99
  • 883
  • 1,009
cagin
  • 5,772
  • 14
  • 74
  • 130
  • Why are you using `POST` instead of `GET`? What do the developer tools tell you? Have you looked at the request headers? Response headers? – Oded Sep 08 '12 at 19:36
  • @Oded — Presumably because the POST verb is more appropriate for the task in question. There is nothing in the question that indicates what the task is, so no reason to assume that POST is inappropriate. – Quentin Sep 08 '12 at 19:38
  • @Quentin - Fair enough. It's just that when I see no data being passed though, I don't feel that post semantics are appropriate. – Oded Sep 08 '12 at 19:39
  • I think the problem is with the way the parameters are defined. – frenchie Sep 08 '12 at 19:40
  • @Oded — It's a cut down minimal test case. – Quentin Sep 08 '12 at 19:40
  • @frenchie, I don't have any parameters. – cagin Sep 08 '12 at 19:42
  • I put an alert instead of ajax call codes, and it doesnt work too. I think the problem is in the $(document).ready(function () ... – cagin Sep 08 '12 at 19:46

1 Answers1

0

The parameter you're specifying is json; but where's the json data?? data: '{}', is an object. Also, I'd check the url parameter. Presumably, you'd need to write your call like this:

var AjaxData = '{"ParameterName":""}';

 $.ajax({
            type: "POST",
            url: "../WebForm1.aspx/GetList",
            data: AjaxData ,
            contentType: "application/json; charset=utf-8",
            dataType: "json",....

And then on the server side, you should therefore specify that you're receiving a string, since that's the format of json data. I would also recommend changing the name of the WebMethod because List can be confusing. And finally, you're returning json, therefore you're returning a string and not an array. Server method like this:

 [WebMethod]
 public string GetList(string ParameterName)
 {
      ...
 }
frenchie
  • 51,731
  • 109
  • 304
  • 510
  • I just made an edit, I think your URL might also be a problem. Also, I just saw that you're not sending parameters; made that edit as well. – frenchie Sep 08 '12 at 19:47
  • aspx and aspx.cs files are in the same root. I add a comment to my question. I think, problem is in the document.ready function – cagin Sep 08 '12 at 19:50
  • Take a look at this answer, might help: http://stackoverflow.com/questions/8405458/return-json-data-from-asmx-web-service – frenchie Sep 08 '12 at 19:52
  • Also, not sure if it's a typo in just the question, but the jquery file reference is missing. If it's also missing from the aspx then thaat would be an issue. – frenchie Sep 08 '12 at 19:56