Two-times encapsulation of jquery-based Ajax methods

Source: Internet
Author: User
Tags http request

I am not a professional front-end development siege Lion, so, the level of this article, may be relatively low, some of the ideas inside, may be more ridiculous. If there are omissions or mistakes, I hope you can point them out. If the value of the article is not big, also hope that Daniel can guide twos, thank you here first.

In the actual development process, because the background only provides interfaces, whether it is Android, iOS or H5 development, by invoking the same set of background interface to obtain data, Android and iOS are provided by the respective system HTTP request method to do the data request. On the H5 side, I used the Ajax method to implement the loading, discarding the back-end rendering scheme like JSP. I'm using the AJAX approach provided by jquery to implement data loading. The AJAX approach provided by jquery has been very concise and practical. But why do we have to do it two times in the package?

In the actual project process, at first, I did not have this idea, I think the Ajax method provided by jquery is simple enough. But when the project was about half way through, it suddenly dawned on me that the Ajax method, the code was quite repetitive, but at this time, I didn't realize what I was supposed to do. Until one day, the background interface found all the requests, missing a key parameter, this time, my H5 side of all the Ajax requests, have to be a manual to add parameters. No way, can only one one manually to add. However, this time I was thinking about a problem, if the late backstage and found missing a key parameter, what should I do. Do you want me to add one more request method to the manual? That's not exhausting me. In addition, if there is an omission, missing one or two request method, then look back to troubleshoot the problem, the time required, also a lot. Can you think of a simple way, if there is a change, I change one place, all the requests are modified synchronously. This time, I think of me in doing Android development, often do some two times the package, perhaps different projects, but a project, I only need to control in one place, you can do the global synchronization changes. Based on this idea, I would like to try it, I can not do two times the Ajax package.

The idea is there, but I have not done before the development of the H5 end, as to whether this idea is feasible, the heart has no end. But if you don't try it, how do you know it is feasible? The big deal is wasting some time. Under this idea, I started my Ajax two-time package.

Of course, since it is a jquery-based Ajax approach to the two-time package, its main content is the Ajax method of jquery, all the requests, and finally the AJAX approach. I just want to do some unified processing, for example, add some uniform request parameters, to respond to the exception, do a unified processing, convenient for me to do a unified processing of requests, but also convenient for unified modification. In the encapsulation method, the load effect is displayed before the request starts, and the load effect is ended when the request finishes (whether the request succeeds or the request is faulted). All my requests, then, will show a uniform loading effect, without having to add the code that shows the load effect before each request, and the code that ends the load at the end of the load. In the request header, add the required request header parameters uniformly. You can also add request parameters uniformly in the request data. In response to the success of the method, in addition to the end of the loading effect, I also do a unified processing of the returned JSON data, the exception response can be disposed of here, do not need to deal with each interface of the business layer alone ... Because the requirements for each project are different, the package for Ajax may not be exactly the same for each project, but they are all the same. Of course, the specific package also needs to be flexibly handled according to the specific business requirements. Also enclosed is my Code snippet


Perhaps Daniel has done such a job, but because of my technical level is not enough, not enough to read other works, so, this package, and no reference to other articles, is completely by the actual business needs to force out, if there is no business change, maybe I would not think of doing such a job. But anyway, this is one of my attempts. If there is a better solution, welcome to explore together. I hope you crossing to enlighten us, thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.