I recommend a useful online Markdown editor, which is much more convenient than compiling it into markdown by myself using Python.
[]
Markdown grammar tutorial
[/markdown/]
Good things will be promoted for free, and I hope I can make several useful gadgets myself.
1. Use of JSONP
jsonp is a data format used to solve cross-domain problems.
For example, if a QR code is needed, the function of generating a QR code already exists. Of course it can be generated
Copy a set of QR codes into . But this causes duplication of code, and this solution is not advocated. So, I adopted
Provides a scheme for API calls.
Interfaces called by JS have encountered cross-domain problems. Normal ajax request
The code is as follows:
$.ajax({ type : "post", url : '/api', dataType : 'json', data : {'id' : 5, 'type' : 3}, beforeSend : function(){}, success : function(returnMsg){ // }, error : function(){ } });
During use, if you are using firefox, the firebug plug-in will prompt that it cannot request across domains.
I have heard that JSONP can solve cross-domain call problems, but I have never encountered cross-domain scenarios and have never used them. Until I was tossing my blog, because the routing function of the blog code was incomplete, I encountered cross-domain problems when I used asynchronous requests to output data. Search for "JSONP" directly and compare it with relevant information to solve the problem. I still can't
Write it out, if I need to solve the same problem, I still need to look for demo and copy it to modify it. But I remember, the keyword is
$.getJSON
It is used to read data.
A few days ago, when I encountered similar problems at work, I happened to review JSONP. However, in this scenario, data is not read, but written. The demo found online is like this:
$.ajax({ type : 'post', url : '/api', dataType : 'jsonp', data : {'username' : 'cg', 'action' : 'add'}, beforeSend: function(){}, success : function(returnMsg){ if(){ //Note that there is no difference here with ordinary ajax requests//do something }else{ //do anthorthing } }, error : function(){ } });
This is the client, and the server's code is like this:
$callBack = isset($_GET['callBack'])?$_GET['callBack']:''; $returnMsg = [ 'code' : 1000, 'success' : true, 'message' : 'Nothing is difficult if you put your heart into it!', ]; $json = json_encode($returnMsg); echo $classBack . '(' . $json. ')';
Remember that it is not accurate, and the correctness of the above code cannot be guaranteed, but the key points have been written out. Client code may miss more points.
In the above scenario, in addition to using JSONP to solve cross-domain, there is another solution.
Write an interface, call the provided interface in this interface, and then request it with normal ajax
to call the interface provided in . This avoids cross-domain.
This solution leads to a problem. When I first encountered ajax calling interfaces across domains, I was very confused: I had called interfaces from other different domain names before, so why didn’t I encounter cross-domain problems? After thinking about it for a while, I learned the reason: the previous calling interface was done using curl and other methods, not JS; cross-domain exists in JS.
When it comes to cross-domain, I encountered cross-domain problems in my work, and I need to configure a nginx server. According to the browser's cross-domain prompts, you can get a lot of similar solutions by searching, but these solutions do not work. Later, through searching, an effective solution was obtained. I don't remember the specific configuration code. Its function is to do the files that need to be cross-domain (such as fonts).
Just configure cross-domain and set the directory where these files are located. nginx's site directory is not the directory of these cross-domain files.
I admire the Internet master who solved that problem. Based on the log, he boldly assumed to try to verify and solve the problem. Unlike me, I can only search for ready-made solutions.
Let’s add the code to solve cross-domain fonts to this article.
Not only that, the knowledge points about jsonp should also be supplemented and corrected. Blogs should not only play the role of outputting knowledge, but also try to ensure their correctness.
2. Batch operation function
The batch operation of the list is an asynchronous request. Two types of data need to be submitted to the server. One is the identification of the data to be operated, such as PK, and the other is the operation type, such as deletion.
This request is triggered by the OK button.
Get the identification of the data to be operated, and it needs to be traversed.
<input type="checkbox" name="id[]" />
JS knowledge points used: traversal, judge whether to select, and obtain the input value.
Get the operation type, need to get
<select> <option value="show">Show</option> <option value="update">Update</option> </select>
I can write these operations quickly, and I don’t even have a 70% sure. Of course, with the help of search engines, I can handle them faster.
The above is the complete description of JSONP and batch operation functions introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply you in time. Thank you very much for your support for my website!