Just because the $.ajax method takes a object parameter of dataType: json, does not mean jQuery is actually sending JSON to PHP as it should be (with the correct media type in the Conent-Type header). Unless PHP 7 has been updated to see the application/json media type, I suspect that jQuery is fudging it here, perhaps converting to URL encoded key/value pairs, as it is well documented that the php://input stream wrapper is required to get data sent via an HTTP POST request method, that is not also sent with the following Content-Types: application x-www-form-urlencoded or multipart/form-data.
The dataType: json property and value of the settings object does not tell jQuery what data format it should send to the server. It merely tells jQuery what kind of data to expect in response! This example is not actually sending JSON to PHP. Read the jQuery API. $.ajax() is converting JSON to URL encoded key/value pairs. If you want to actually send true JSON with $.ajax(), then you should set the contentType: property of the setting / parameter object to 'applicaton/json'. Additionally, you MUST set processData: to false. Relying on jQuery seems less and less like a good idea for web developers to use.
As you can see on 5:12 Alex has a form with the action "add.php". If he wouldnt return "false" and would press the button the browser would open up "add.php" and send the input information. But as we dont want to load a new page, he catches the action and loading the data in the background via ajax.
@JellybeanGames It's not just so that he can call PHP code without loading the page. That is secondary to the issue. If he were to use standard, synchronous code (say from an HTML form), then there would be no opportunity to send JSON because everything would be sent in the form of URL encoded key/value pairs in the body of the HTTP request. The actual XmlHttpRequest object in JavaScript can do both synchronous and asynchronous HTTP requests. Moreover, it allows you to set the media type value for the all important HTTP Content-Type header. By default, PHP does not see the application/json media type (in the Content-Type header) and $_POST will not contain any data. Just because the $.ajax method takes a object parameter of dataType: json, does not mean jQuery is actually sending JSON to PHP as it should be (with the correct media type in the Conent-Type header). Unless PHP 7 has been updated to see the application/json mediat type, I suspect that jQuery is fudging it here, perhaps converting to URL encoded key/value pairs, as it is well documented that php://input stream wrapper is required to get data sent via the HTTP POST request method that is not also sent with the following Content-Types: application x-www-form-urlencoded or multipart/form-data.
The MIME type of JSON should be application/json - However if you provide a callback (also known as JSON-P) you use the MIME type of text/javascript, because it's a javascript function that is returned instead of an object.
what if i want to send ajax code thathas json string to php file so i can convert it there into php array how can i do that while i tried but failed many times
Please do a proper php socket tutorial. For example how to make a simple live chat using sockets. Its hard to find a good and easy to understand tutorial
Alex, its time for an introduction to Web-sockets, I and others with me are thinking of using Web-sockets for Chat application and other social plugins. It would be great if you do a tutorial on this, Or a simple installation and setup guide would be enough too...TY
how do you json_encode a URL? I am having problems with the backslash. If I encode this to json " echo json_encode("foo//bar) " I get this "foo\/\/bar" in json. How do I escape the // so it encodes correctly to json? thanks and I love your tutorials. Jon
I tried using the new .done instead of success and nothing works. my echo from 9:55 is showing in the loaded page at the top which doesn't happen in the example for some reason and my console is empty even though i'm doing a console.log(data); inside the done anonymous function.
Hey Alex, nice video. I was watching this as a reference and I noticed that you are using the the 'success' method for success, but that has now been replaced with the 'done' function since jQuery 1.8; So I think you should update the video so that beginners might not experience problems.
How is it possible to encode Hebrew? I was trying to pass php arrays into JavaScript arrays by json_encode. At first it worked but later stopped and showed error. I don’t know why. The Hebrew letters were translated into a series of numbers and letters in the beginning.
hello dear developer, good evening. Want to work with Json - i i want to translate the following overopass-turbo request [out:csv(::id,::type,"name","addr:postcode","addr:city","addr:street","addr:housenumber","website"," contact:email=*")][timeout:600]; {{geocodeArea:Schweiz}}->.a; ( node(area.a)[amenity=hospital]; way(area.a)[amenity=hospital]; rel(area.a)[amenity=hospital];); out;
Well, of course i would need some JS for that, but still, mainly using sockets and socket servers. Just some tutorial that has a good explanation. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-69OXnmbAaLI.html