Problems using jQuery $.ajax to pass data

Posted by iboeno on Stack Overflow See other posts from Stack Overflow or by iboeno
Published on 2010-03-12T21:22:08Z Indexed on 2010/03/12 21:27 UTC
Read the original article Hit count: 228

Filed under:
|
|
|
|

I'm using ASP.NET and attempting to call a method with a signature of

[WebMethod] 
public static string GetInfo(string id){...}

using the following javascript:

var elementValue = $("#element").attr('id');
var d = "{id : " + elementValue + "}";
$.ajax({
            type: "POST",
            url: "../WebPage.aspx/GetInfo",
            data: d,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                //do this
            }
        });

And this is not working. If instead I set elementValue = 2; it works fine. If I try to hardcode in a string value for testing purposes e.g. elementValue = "nameToLookUp"; It fails. Why is this happening, and how do I resolve it?

On a side not, why is type: required to be POST instead of a GET? In the end I just want to pass a string value I want to look up in a DB and retrieving some json data.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-ajax