user1416156
user1416156

Reputation: 3023

How to pass parameters to an ajax call

I have a javascript class called MyTest.js, now I register this script on the load of my MyWebPage.cs, But i need to pass 3 parameters to the function which it will then make a call to a web service. Here is my javascript function

MyTest.init = function() {
    var params = new Hash();
    params.set("blank", "blank");
    new Ajax.Request('/Test.asmx/CreateClients', {
        method: 'post',
        onSuccess: attachCallback(this, "Yay"),
        onFailure: attachCallback(this, "Nay"),
        onException: attachCallback(this, "Nay"),
        parameters: params
    });
}

Now the parameters i need to pass are two stings, name and notes which I can get in the load of my .cs file. How can I pass the values to this init function?

Thank you

Upvotes: 2

Views: 2152

Answers (3)

Dave Archer
Dave Archer

Reputation: 3060

You could try, on the server side writing out

<script type="text/javascript">
 var param1 = '<%= dotNetValue %>';
 var param2 = '<%= otherDotNetValue %>';
</script>

then on the client you'll have access to those (global) variables

params.Set("first", param1);
params.Set("second", param2);

Upvotes: 0

dontGoPlastic
dontGoPlastic

Reputation: 1782

It looks to me like you're using Prototype, not jQuery, but I'm not certain. If you are, you just need to populate your Ajax.Request's parameters object with your name and note properties.

MyTest.init = function(name, note) {
    var params = new Hash();
    params.set("blank", "blank");
    params.set("name", name);
    params.set("note", note);
    new Ajax.Request('/Test.asmx/CreateClients', {
        method: 'post',
        onSuccess: attachCallback(this, "Yay"),
        onFailure: attachCallback(this, "Nay"),
        onException: attachCallback(this, "Nay"),
        parameters: params
    });
}

Upvotes: 0

John Koerner
John Koerner

Reputation: 38077

Use the data property:

MyTest.init = function() {
    var params = new Hash();
    params.set("blank", "blank");
    new Ajax.Request('/Test.asmx/CreateClients', {
        method: 'post',
        data: {Name:"name", Notes:"Notes"},
        onSuccess: attachCallback(this, "Yay"),
        onFailure: attachCallback(this, "Nay"),
        onException: attachCallback(this, "Nay"),
        parameters: params
    });
}

Upvotes: 2

Related Questions