c# - passing multiple parameters to .asmx from jquery ajax GET -


html

<a onclick="testgetparametersdynamic2();">fill in names , check out</a> <br /> <p>enter first name</p> <input id="myfirstname" type="text" /> <br /> <p>enter last name</p> <input id="mylastname" type="text" /> <div id="outputget3"></div> 

c#

[webmethod(enablesession = true)] [scriptmethod(usehttpget = true)] public string testgetparametersdynamic(string firstname, string lastname) {     string fullname = firstname + lastname;      return fullname; } 

i have tried multiple ways of entering data bc think problem lies

attempt 1

function testgetparametersdynamic2()     {         $.ajax(         {             post: 'get',             contenttype: 'application/json; charset=utf-8',             datatype: 'json',             data: '{"firstname":"' + $('#myfirstname').val() + '","lastname":' +                     $('#mylastname').val() + '"}',             url: 'utilitieservice.asmx/testgetparametersdynamic',             success: function (result)             {                 var test = result.d;                 var outputdiv = $('outputget3');                 outputdiv.html(test);             },              error: function ()             {                 alert('fail test dynamic');             }         });     } 

attempt 2:

function testgetparametersdynamic2()     {         $.ajax(         {             post: 'get',             contenttype: 'application/json; charset=utf-8',             datatype: 'json',             data: "firstname" + $('myfirstname').val() + "&lastname" + $('mylastname').val(),             url: 'utilitieservice.asmx/testgetparametersdynamic',             success: function (result)             {                 var test = result.d;                 var outputdiv = $('outputget3');                 outputdiv.html(test);             },              error: function ()             {                 alert('fail test dynamic');             }         });     } 

both times error:

invalid web service call, missing value parameter: \u0027firstname\u0027

i hope use [scriptmethod(responseformat=responseformat.json)] attribute web method or set same information in web.config in case of usage .net 4.0.

it seems me first attempt correct, should replace

data: '{"firstname":"' + $('#myfirstname').val() + '","lastname":' +                     $('#mylastname').val() + '"}', 

to

data: '{"firstname":"' + $('#myfirstname').val() + '","lastname":"' +                     $('#mylastname').val() + '"}', 

(the starting double-quote skipped before $('#mylastname').val()).

i strictly recommend don't use manual serialization json. example if text $('#myfirstname').val() or $('#mylastname').val() have '"' or '\' characters, characters must escaped additional backslash ('\') (see here). instead of manual serialization should use json.stringify function script json2.js can download http://www.json.org/js.html or here. in recent web browsers function native implemented , json2.js use native implementation if take place.

the data parameter of $.ajax rewritten following:

data: {     firstname: json.stringify($('myfirstname').val()),     lastname: json.stringify($('mylastname').val()) } 

or in situation as

data: {     firstname: function() { return json.stringify($('myfirstname').val()); },     lastname: function() { return json.stringify($('mylastname').val()); } } 

for more information see this old answer , this.

updated: sorry correct version without usage json.stringify without data usage:

url: 'utilitieservice.asmx/testgetparametersdynamic?firstname=' +      encodeuricomponent('"' + $('#myfirstname').val() + '"') +      '&lastname=' + encodeuricomponent('"' + $('#mylastname').val() + '"') 

i strictly recommend use json.stringify version described above.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -