Analysis of problems with ajax requests for data, and problems with ajax requests for data

Source: Internet
Author: User

Analysis of problems with ajax requests for data, and problems with ajax requests for data

Use jquery and post to request data: JSON. stringify (data)

$.ajax({ type:"POST", async : false, url:"../../gzq/circle/deleteTeam", dataType:"json", contentType:"application/json; charset=utf-8", data:JSON.stringify(c), success:function(data){  console.log(data); }, error:function(error){  console.log(error); }});

Cause: in fact, if JSON. stringify (data) is not used, it will become a String concatenation, 'name = vinxent & age = 21', a bit similar to the get method. If JSON. stringify (data) is used, the json object -- "{name; 'vinxent ', age: 21}" will be transmitted }"

Therefore, in general scenarios, the get method does not require JSON. stringify, and the post method does.

AngularJS request Summary

$ Q usage of delayed loading (promise in angularJS)

.service('findXfzzShopInfo', ['$http', '$q', function($http, $q) { this.get = function(shopClassId, val) {  var def = $q.defer();  $http({   url: '../../shop/findXfzzShopInfo?shopClassId=' + shopClassId + '&val=' + val,   method: 'GET',   cache: true  }).then(function(resp) {   def.resolve(resp.data);  }).catch(function(err) {   def.reject(err.data);  });  return def.promise; };}])

To determine the environment, the request is encapsulated as a service and the $ q defer delayed loading is used.

Cache: true Configuration

When the request content remains unchanged for a long time, we can use this configuration to cache the request, reduce the server pressure, increase the page speed, and clear the cache when the page is closed. You can also configure the time after cahe.

When the request is 200, you find that the function fails.

What we will talk about here is not to configure responseType, but to return a list in the background during the get request. When there is a value, you will find that there is no problem at all, when there is no value, nothing will be returned in the background, and your responseType is json. At this time, the failed function is entered, so you are stuck in the background, you can return an object if you leave it empty in the background.

The above are the problems we have encountered in this ajax request. If you still have any questions you may not understand during your study, you can discuss them in the message area below. Thank you for your support for the help house.

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.