We all get stuck from time to time with broken code. We know what it needs to do, but don't know why it isn't doing what we expect.
Don't write 20 or 30 lines of code without testing what you have written.
Batch your work into smaller units to understand what is going on.
Bugs are easier to find when they are small.
Most error messages by your editor or browser point you to the exact line that is broken.
If you can't tell what's going on, copy and paste that into Google.
Example from Exercise 17...deconstruct your code in dev tools console
Stands for JavaScript Object Notation. It is object syntax that is standardized by the same organization that standardizes JavaScript.
JSON is frequently used to communicate data between programming languages. XML (extensible markup language) was primarily used before JSON.
A JSON file has the extension of .json. It is frequently used for configuration or smaller data sets.
{
"name": "Jon",
"isAwake": true,
"count": 3,
"languages": ["JavaScript", "Java"],
"job":{
"field": "Development",
"responsibilities": ["manager"],
"boss": {
"name": "Zac McCool",
"title": "Senior Manager"
}
}
}
Must start and end with { } Must use quotation marks. Ticks don't work. Cannot have comments
var jsonString = "{\"name\": \"Jon\"}";
var jsonObj = JSON.parse(jsonString);
console.log(jsonObj.name);
var obj = {name: "Jon"};
var jsonString = JSON.stringify(obj);
console.log(jsonString); //"{\"name\": \"Jon\"}";
JavaScript calls a url and the URL returns XML.
Most web services return JSON now a days.
jQuery provides us a very easy and quick way to perform ajax calls.
$.ajax({
url:'',
method:'GET|POST|PATCH|PUT|DELETE',
success: function(response){},
failure:function(response){}
})
GET
- Retrieve POST
- CreatePATCH
- ModifyPUT
- UpdateDELETE
- DeletePATCH updates existing, PUT replaces altogether. Remember it as CRUD operations.
$.get('blah.json', function(response){
});
If you use jsfiddle and want to simulate ajax calls, use /echo
So far, this has been the primary method for adding content to an html element.
var errors = $('.error');
var currentHtml = errors.html();
//Add to the end
errors.html(currentHtml + 'new error ');
//Adds to the beginning
errors.html('new error ' + currentHtml);
var errors = $('.error');
//Add to the end
errors.append('new error ');
//Adds to the beginning
errors.prepend('new error ');
Do your best to not use these methods inside of a loop. Use them after a loop has completed. Why?
for(var i = 0; i < errors.length;){
$('error').append('This is an error');
}
var errorList = ''
for(var i = 0; i < errors.length;){
errorList += 'This is my error';
}
$('error').append(errorList);
See the Pen UA-ITR-jQuery-Append by Jon (@Middaugh) on CodePen.
One of the primary methods for sending data from a website to a web server to be processed.
You have primarily seen POST requests on HTML forms.
$.post('[url]', 'object to send' , function(response){
//
});
$.post('api/user', {userName: 'Jon'} , function(response){
//
});
There's lots of typos, issues with selectors, etc in part 1. These are on purpose to give you debugging practice.