jnsohnumr
jnsohnumr

Reputation: 169

How do I pass MANY variables via getJSON to my WebService?

I would like to pass numerous variables to a web service (around 60), and I'm wanting to know if there is a better way to accomplish this. At the moment, I'm using a GET and am able to pass parameters via the URI (e.g. blahblah.com/People/{person_id} ) and while that works great for something like passing an ID, with so many variables it will look bad and be a hassle. I understand that by doing a POST instead it would allow my to pass my many variables without the need to create a gigantic URL and just let the WebService handle the mapping. I would like to do this, however just changing my Method value from GET to POST in my [WebInvoke(...)] annotation is coming up with a "not allowed" type message. I'm sure that there is something simple I'm missing and would love to get some insight. Also, if I just pack all of my variables into a JSON object and pass that as the "data" argument for getJSON, it shows all of my variables, but I don't have any way to access them from my server side code.

In case the preceding didn't make it obvious for my Server side I'm using C# in Visual Studio 2008 for my RESTful web service, which is targeting .NET Framework 3.5. I believe the application is a "Web Service", as the actual code lives in a file named like MyRestService.svc.cs.

For my Client side I'm using standard HTML/CSS/JS/jQuery (I'm NOT using aspx on Client side). To pull from the Server, I'm using the $.getJSON() function.

Please let me know what information I'm leaving out that could help you help me.

Thanks for looking,

jnsohnumr

Upvotes: 1

Views: 551

Answers (2)

StriplingWarrior
StriplingWarrior

Reputation: 156524

Changing the [WebInvoke] annotation will set you up right on your server side, but you're still telling jQuery to do a GET from the browser. According to the docs, $.getJson is shorthand for:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

So you can replace your $.getJson call with a call to $.ajax, but with an additional parameter to make it use POST:

$.ajax({
  url: url,
  type: 'POST', // Use POST instead of GET
  dataType: 'json',
  data: data, // A javascript object with fifty variables on it.
  success: callback
});

PS--if you're passing fifty variables around in a web request, it's a sign that you may be using the request for something that belongs elsewhere (like the user's session). You may want to reconsider your entire approach.

Upvotes: 1

Mitya
Mitya

Reputation: 34556

For GET requests, even though you package your data up neatly into a JS object on the client, ultimately it gets sent as file.php?var1=1&var2=2 etc.

This is fine, except:

  • servers can sometimes respond with "URL too long" error codes (I forget the actual code)
  • browsers have their own limits on URL lengths (IE, for example, is, or at least used to be, 2083 chars)

There is also the security implications of GET, but if you're considering GET at all I'm guessing you're across that.

POST, of course, avoids such problems. The best course of action really would be to get the server to accept POST, but I don't know how viable that is for you.

Back to GET... I don't know C# but in PHP there is a GET superglobal that would receive all these incoming variables and packs them into an associative array.

$_GET; //array of incoming GET vars

If there is not something similar in C# then your best bet would be to deserialize into some other structure on the server.

Hope any part of this ramble helper.

Upvotes: 0

Related Questions