Areas for improvement in simple Simple AJAX request code / Looking for a good code templateapplication
My code looks like this:
Do I need to somehow close the connection? It looks like send()
send()
automatically closes the connection because I cannot execute send()send()
twice.Is the error handling ok? When my internet connection was a bit unstable, I got the message "Error: 0". Zero sounds like a strange status code.
Note: For now, I'd like to stick with plain JavaScript, without jQuery or other frameworks.
Thank you very much.
UPDATE: Explanation/Contextcontext of the code:
The HTML page has a table. Each table row represents a dataset with an ID. The id of each row is "line" plus the dataset ID, e.g. "line1".
Each Each table row has a link named "Solve" which calls the JavaScript function _solve(id)
.
Areas for improvement in simple AJAX request code / Looking for a good code template
My code looks like this:
Do I need to somehow close the connection? It looks like send() automatically closes the connection because I cannot execute send() twice.
Is the error handling ok? When my internet connection was a bit unstable, I got the message "Error: 0". Zero sounds like a strange status code.
Note: For now, I'd like to stick with plain JavaScript, without jQuery or other frameworks.
Thank you very much.
UPDATE: Explanation/Context of the code:
The HTML page has a table. Each table row represents a dataset with an ID. The id of each row is "line" plus the dataset ID, e.g. "line1".
Each table row has a link named "Solve" which calls the JavaScript function _solve(id)
.
Simple AJAX request application
Do I need to somehow close the connection? It looks like
send()
automatically closes the connection because I cannot executesend()
twice.Is the error handling ok? When my internet connection was a bit unstable, I got the message "Error: 0". Zero sounds like a strange status code.
Note: For now, I'd like to stick with plain JavaScript, without jQuery or other frameworks.
Explanation/context of the code:
The HTML page has a table. Each table row represents a dataset with an ID. The id of each row is "line" plus the dataset ID, e.g. "line1". Each table row has a link named "Solve" which calls the JavaScript function _solve(id)
.
Improvement suggestions Areas for firstimprovement in simple AJAX applicationrequest code / Looking for a good code template
Today I created my first simple AJAX application. It works fine, but I am a bit unsure if I did everything correct. I'd like to use the code as template for future projects.
UPDATE: Explanation/Context of the code:
Improvement suggestions for first AJAX application
Today I created my first simple AJAX application. It works fine, but I am a bit unsure if I did everything correct.
UPDATE: Explanation of the code:
Areas for improvement in simple AJAX request code / Looking for a good code template
Today I created my first simple AJAX application. It works fine, but I am a bit unsure if I did everything correct. I'd like to use the code as template for future projects.
UPDATE: Explanation/Context of the code:
UPDATE: Explanation of the code:
The HTML page has a table. Each table row represents a dataset with an ID. The id of each row is "line" plus the dataset ID, e.g. "line1".
Each table row has a link named "Solve" which calls the JavaScript function _solve(id)
.
<tr id="line61087">
<td>php/error.log</td>
<td><a href="javascript:_solve(61087)">Solve</a></td>
<td>1113</td>
<td>2017年09月03日 01:18:18</td>
<td>Xyz...</td>
</tr>
When the user clicks at "Solve", a PHP script (ajax_cmd.php) is called with AJAX. The PHP script deletes the dataset and confirms the deletion with the JSON object success: true; id: (the ID)
on success or success: false; error: (the error message)
on failure.
After the PHP script confirmed the successful deletion, the AJAX script hides the table row and decreases a row counter.
A double-click prevention (which might result in a double-decrease of the counter) is realized with a check if the row is already hidden.
UPDATE: Explanation of the code:
The HTML page has a table. Each table row represents a dataset with an ID. The id of each row is "line" plus the dataset ID, e.g. "line1".
Each table row has a link named "Solve" which calls the JavaScript function _solve(id)
.
<tr id="line61087">
<td>php/error.log</td>
<td><a href="javascript:_solve(61087)">Solve</a></td>
<td>1113</td>
<td>2017年09月03日 01:18:18</td>
<td>Xyz...</td>
</tr>
When the user clicks at "Solve", a PHP script (ajax_cmd.php) is called with AJAX. The PHP script deletes the dataset and confirms the deletion with the JSON object success: true; id: (the ID)
on success or success: false; error: (the error message)
on failure.
After the PHP script confirmed the successful deletion, the AJAX script hides the table row and decreases a row counter.
A double-click prevention (which might result in a double-decrease of the counter) is realized with a check if the row is already hidden.