Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Patrick Koch
40,627 Pointsjquery getJSON AJAX request
Hello, community
How can I send data to the server with a jquery.getJSON request?
from the jquery doc I got: " jQuery.getJSON( url [, data ] [, success ] )´; Load JSON-encoded data from the server using a GET HTTP request.
Data that is sent to the server is appended to the URL as a query string. If the value of the data parameter is a plain object, it is converted to a string and url-encoded before it is appended to the URL. "
So I wanted to send a String to the url, how can I access that String on the url?
greetings patrick
3 Answers
Iago Wandalsen Prates
21,699 PointsThere is actually two ways, server-side and client-side. You can do it like you said in PHP, (in other languages it would be different) that is the server side, and how to do it depends on what backend language you are using.
The other way is client side. On your javascript, you can use document.URL to access the URL, than you'd want to do something like:
var query = document.URL.split("?")[1] // split between the first part of the URL and the query
var data = query.split("&") // if there are multiple data being sent, split it into a array
You'd probably still have to parse the resulting string to make use of it
Patrick Koch
40,627 Pointsok got it
jQuery.getJSON( url [, data ] [, success ] )´;
the String "data", can be accessed on the url with $_GET["data"]
g patrick
Patrick Koch
40,627 PointsI was sending a plain js object per ajax to a php file where process it further and returns a json array, I forgot to check the console, there you can see how url looks like, and with $_GET[] its no problem to get it out.
but thx for info lago
g patrick