Front-end ajax interaction posture and backend interaction posture
Ajax is usually used to exchange data with the backend.
However, there are many ways to interact, many of which depend on the attributes of your backend. Here I mainly list the two methods that are commonly used in my current project.
-- One is our general web api and controller,
First, let's take a closer look at the interaction data between ajax and webapi.
Here, we will briefly describe the four attributes of web APIs-GET, POST, PUT, and DELETE.
Here, yongchang uses GET and POST.
If AJAX is used for backend interaction with the data
$. Ajax ({url: "Your webapi", type: "Get", DATA {}, // write the DATA you want to pass to the backend, but the GET method is generally not required, directly read success: function (data) {// return event after successful} error: function (data) {alert ("data "); // error reporting after a request error (it is very important. If you do not know where to change the error, this will help you. I personally think it is mainly 500 Error Reporting )}})
This kind of GET generally reads some data from the backend during webpage initialization and then assigns it to the page (maybe my terminology is not standard, forgive me for QAQ)
Then I want to talk about the POST method.
$. Ajax ({url: "Your webapi", type: "POST", DATA {}, // write the DATA you want to pass to the backend, it is a bit important that you must add [HTTPPOST] To Your webapi method. Otherwise, 500 error will be reported: success: function (data) {// return event after success} error: function (data) {alert ("data"); // error report after a request error (very important. If you do not know where to change the error, this will help you, I personally think it is mainly 500 error )}})
The POST method is the same as the ajax method. You only need to remember to add [HTTPPOST] To Your WEBAPI method.
The rest I want to summarize some POST-to-controller methods that are sometimes used. Although the controller can directly URLPOST, I personally prefer AJAX, the main reason is that AJAX can see where the psot error is 233
$. Ajax ({url: Your Controller/your Controller method name, type: 'post', dataType: 'json', data :, // The parameter value contentType: "" application/json "is still to be passed. // here we will tell you that the parameter must be in json format. success: function (data) {// returned event after success }});
It is worth mentioning that the values I personally pass are of the json type. At present, no tests have been conducted to pass non-json parameters;
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!