The first time I touched the. Post () method and the. Get () method in jquery Ajax, I felt the simplicity and power of Ajax, when using the. Post () method, to find the relevant usage on the W3, the feeling is very simple, the usage is very clear, and then, Directly according to their own needs (JS post data to PHP to write to the database, and then return to JS a value) write, finished, run, unexpectedly not! Then carefully compare my code with the demo, in order to find the error, my code is almost the same as the demo, but still can't get PHP data in JS.
Here, let's recall the. Post () method.
Jquery.post (url,data,success (data, Textstatus, JQXHR), DataType)
Parameters |
Describe |
Url |
The URL to which the request is sent |
Data |
Data sent (optional) |
Success (data, Textstatus, JQXHR) |
After successful execution of the callback function, parameters are optional, data is returned Textstatus is the state of a function, or you can add jqxhr to get a function The return status |
DataType |
Expected server corresponding data type |
After careful comparison, found that JS code can no longer change, and then I began to look at the PHP code, found that the only difference with the internet is that I added a database insert operation, is this the reason? I put the connection database, insert the database operation all commented out after, succeeded ~ ~ ~ (certainly ah, with the online demo can no longer be the same).
This is why, and then began to look at the information on the Internet, suddenly see the post () function of the time problem, the general meaning is that when I URL connected to the PHP interface inserted into the database, it takes a certain amount of execution time to insert the database, when my click function executed, refreshed, The callback function does not have the chance to perform the post, so no matter how I refresh it does not change.
$.ajax ({url:c_url, timeout:+, function(data) {alert ("AAA");} });
Adding the timeout limit, after testing, found that function is also able to execute, it is clearly the problem of timeouts. Inserting the database slows down the post execution successful callback, so I add the connection database and the insert database to a new PHP page separately and then get the data.
This is my last JS code:
1$ (document). Ready (function(){2$ ("a"). Click (function(){3 varC_url = ' http://localhost/test/check.php '; 4 varJJ;5$.post ("db.php", {ID: "5"}); 6$.post ("check.php", {},function(data) {jj=data;$ ("#tname"). HTML (data);}). Successfunction() {alert ("second Success"); })7. Error (function(a) {alert ("error" +a.responsetext); alert (Json_last_error ()); })8. Complete (function() {Alert ("complete"); });9$ ("#tname"). HTML (data);Ten }); One A});
Where check.php has only a separate defined array returned for testing if it is received.
At this point, is the end, but this implementation, the HTML method changed the contents of the label, but on the page to display a sudden refresh no, has not been resolved. I hope you have the great God guidance ~
The first contact with Ajax has encountered such a problem, the future still need to learn the system.
An issue in which the callback function does not execute in the Post method in Ajax