Home > Web Front-end > JS Tutorial > What problems will you encounter when requesting data with ajax?

What problems will you encounter when requesting data with ajax?

php中世界最好的语言
Release: 2018-03-15 10:22:56
Original
1623 people have browsed it

This time I will bring you what problems you will encounter when requesting data with ajax. What are the precautions for ajax requesting data? Here are practical cases, let’s take a look.

Use jquery,

post request data: where to use 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);
 }
});
Copy after login
Reason: In fact, if JSON.stringify(data) is not used , will become

string concatenation, 'name=vinxent&age=21', which is somewhat similar to the get method. If you use JSON.stringify(data), the json object will be transmitted-"{name;'vinxent', age:21}"

So, in general scenarios, the get method does not require JSON.stringify. Post method requires.

angularJSRequest some summary

$qThe use 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;
 };
}])
Copy after login
Due to To judge the environment, encapsulate the request as a service and use $q's defer delayed loading.

cache: true configuration

When the request content has not changed for a long time, we can use this configuration to cache the request, reduce the pressure on the server, and increase the speed of the page. This cache can be cleared when the page is closed. And the time can also be configured behind the cache.

When the request is 200, you find that the failed function is used

What I want to talk about here is not to configure the responseType, but when I get the request in the background Return a list. When there is a value, you will find that there is no problem at all. When there is no value, the background returns nothing, and your responseType is json. At this time, the failed function enters, so the background still cheats you, and The background will negotiate and return an object to you when it is empty.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

jQuery selector gets all parent elements and following elements at once

How about jquery+ajax Implement data update

Realize jquery to load data immediately on the startup page

Use jquery to operate the text content of the input box

The above is the detailed content of What problems will you encounter when requesting data with ajax?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template