0

I am trying to get a data string from a jQuery POST to a aspx page. But no luck.

This is my jQuery

function namePerson() {
    $.ajax({
        type: "POST",
        url: "names.aspx",
        data: {name : "Anders"},
        beforeSend: function() {
            $("#ViewContainer").html("<img src='loading.gif' />");
            },
        success: function(msg){
            $("#ViewContainer").html(msg);
            }
    });
}

And in the beginning of the aspx I have this

string strName = Request.QueryString["name"];

I can see the 'name' string is sent via debug in browser. But I am not able to retrieve it. I am coming from classic ASP and are trying to learn ASP.net C#. I have not been able to find any search result explaining how to do this.

Help much appreciated =)

0

4 Answers 4

2
function ShowCurrentTime() {
$.ajax({
    type: "POST",
    url: "CS.aspx/GetCurrentTime",
    data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    failure: function(response) {
        alert(response.d);
    }
});
}

function OnSuccess(response) {
alert(response.d);
}

In C#-----

[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name){
return "Hello " + name + Environment.NewLine + "The Current Time is: "
    + DateTime.Now.ToString();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should use a WebService.

[WebService, ScriptService]
public class WebService1 : WebService
{
    [WebMethod]
    public string names(string name)
    {
        return "Hello " + name;
    }
}

Then call it from client-side using Javascript.

function namePerson() {
    $.ajax({
        type: "POST",
        url: "WebService1.asmx/names",
        data: JSON.stringify({ name: "Anders" }),
        contentType: "application/json; charset=utf-8",
        beforeSend: function () {
            $("#ViewContainer").html("<img src='loading.gif' />");
        },
        success: function (msg) {
            $("#ViewContainer").html(msg.d);
            alert(msg.d);
        }
    });
}

Please make sure contentType: "application/json; charset=utf-8" is present, else the web-service will use XML to respond.

Also, you should use done instead of success.

Comments

0

You are calling JQuery.AJAX incorrectly. This line is incorrect.

url: "names.aspx",

I think there might be some method within your page names.aspx which you are trying to call. Consider you've a page like names.aspx and within that page(code behind file), you've a method which gets names based on your provided data, so the method definition and declaration would be like

[WebMethod]
public void GetNames(string name)
{
        ...
}

And your JQuery AJAX call url will be like

url: "names.aspx/GetNames",

Hope it helps you understand how you can call the code behind method view JQuery.AJAX

3 Comments

This works, only that I want to use the string in another Method. I googled and a void can't return anything. I tried making it 'public static string'. Then i can set 'return strName;' But I can't call the string in another Method. Yes, I am a newbie trying to get my head around this :)
Yes, void returns nothing, it depends what you want to do with that method call via ajax. You can call whatever method you need to call, the method can not only return string but it can also return Json and other data types too.
The ajax call only returns a page for users to upload a file. I need to pass the string 'name' because I manually renames the uploaded files and use this string. All I want to do it pass it on and be able to use in a Method I have for uploading the file. I tried to move the '[WebMethod] public..' into the Method triggered by the upload button. But that made a CS1513: '}' expected fault. I post the code here: pastebin.com/XLkdcceW
0

on Same Page CodeBehind do like this as written below

CodeBehind: On your names.aspx page add this function, make it STATIC

   [WebMethod]
    public static string names(List<string> obj)
    {
          string result = string.Empty;
           // code Logic
            string n1=obj[0];
            string n2=obj[1];
            string n3=obj[2];
   

        return result;
    }

Now call this function using jquery ajax

JS Code:

function namePerson() {

       var aData = [];
        aData[0] = "John";
        aData[1] = "Andrea";
        aData[1] = "Leslie";
        var jsonData = JSON.stringify({ obj: aData });

        $.ajax({
            type: "POST",
            url:"names.aspx/names",   
            data: jsonData,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess_,
            error: OnError_
        });

}

function OnSuccess_(response){
  var result=response.d;
   alert(result);
}

function OnError_(error){
   alert(error);
}

Comments

Your Answer

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.