The third week -------- json and ajax

You need to be converted into json list when drawing the map

I mainly use the import com.google.gson.Gson; this package

Here is converted code

private void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO 自动生成的方法存根
        //yq cour = new yq();
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        /*response.setCharacterEncoding("UTF-8");
        response.setHeader("content-type","text/html;charset=UTF-8");*/
        String Date = request.getParameter("time");
        List<yq> list =  CourseDao.find(Date);=
        Gson gsonnew Gson();
        String json = gson.toJson(list);
        System.out.println(json);
        response.getWriter().write(json);
    

        
    }

In servle layer can find web applications by way of the output of the error situation

Then ajax function

Mainly in the following parameters

1.url
Requirements of type String parameter (the default address of this page) address of the request.

2.type
requirements parameter of type String, request method (POST or get) defaults to get. Note that other http request method, such as put and delete can also be used, but only part of the browser supports.

3.timeout
Requirements type Number parameter setting request time (in milliseconds). This setting overrides $ .ajaxSetup () method of the global settings.

4.async
requirements for the Boolean type parameter defaults to true, all requests are asynchronous requests. If you need to send a synchronization request, set this option to false. Note that the synchronization request will lock the browser, the user must wait for another request to complete before the operation can be performed.

5.cache
requirements for the Boolean type parameter defaults to true (as when dataType Script, the default is false), is set to false information will not load request from the browser cache.

6.data
requirements for the Object or parameter of type String, the data sent to the server. If you have not a string, it will automatically be converted to string format. get request will be appended to the url. Prevent this automatic conversion, you can view processData options. Object must key / value format, e.g. {foo1: "bar1", foo2 : "bar2"} is converted to & foo1 = bar1 & foo2 = bar2 . If the array, JQuery will automatically correspond to the same name to different values. For example {foo: [ "bar1", "bar2"]} is converted to & foo = bar1 & foo = bar2 .

7.dataType
Requirements for the parameter of type String, the expected data type returned by the server. If not specified, will automatically return JQuery responseXML or http responseText The mime information packet, and the callback function passed as parameters. Available types are as follows:
xml: returns an XML document, available JQuery process.
html: Returns the HTML plain text information; script tag contained performs when inserted DOM.
script: return plain text JavaScript code. It does not automatically cached results. Unless the cache parameter sets. Note that when the remote request (not in the same domain under), all post requests are converted to get the request.
json: returns JSON data.
jsonp: JSONP format. When using the form SONP call functions, such as myurl? Callback = ?, JQuery will automatically replaced after a "?" Is the correct name of the function to execute the callback function.
text: Returns the plain text string.

8.beforeSend :
requirements Function type parameter, can modify the function of the XMLHttpRequest object before sending a request, such as adding custom HTTP header. Returns false if the beforeSend can cancel the ajax request. The XMLHttpRequest object is the only parameter.
            function (the XMLHttpRequest) {
               the this; // Options parameter passed when calling this ajax request
            }
9.complete :
requirements Function type parameter, requesting callback function upon completion (success or failure of the request when the average call). Parameters: XMLHttpRequest object and a type description string successful request.
          function (the XMLHttpRequest, textStatus) {
             the this; // Options parameter passed when calling this ajax request
          }

10.success : Function requirements for the type of parameters, the callback function to call when the request is successful, there are two parameters.
         (1) returned by the server, and performs data processing in accordance with the dataType parameter.
         String (2) described state.
         function (Data, textStatus) {
            // Data may be xmlDoc, jsonObj, html, text like
            this; options // call parameters passed when this ajax request
         }

11.error :
requirements for the types of parameters Function, the function is called when the request fails. This function takes three parameters, i.e., XMLHttpRequest object, an error message, error trapping object (optional). ajax event function is as follows:
       function (the XMLHttpRequest, textStatus, errorThrown) {
          // textStatus and usually contains only one information errorThrown
          this; options // call parameters passed when this ajax request
       }

12.contentType :
requirements parameter of type String, to send information to the server when the content default encoding type "application / x-www-form -urlencoded". The default value is suitable for most applications.

13.dataFilter :
requirements for the types of parameters Function, Ajax returned to the original data preprocessing functions. Provide data and type two parameters. Ajax data is raw data returned, type dataType parameter is provided when calling jQuery.ajax. The value returned by jQuery further processing.
            function (Data, type) {
                // return the processed data
                return Data;
            }

14.dataFilter :
requirements for the types of parameters Function, Ajax returned to the original data preprocessing functions. Provide data and type two parameters. Ajax data is raw data returned, type dataType parameter is provided when calling jQuery.ajax. The value returned by jQuery further processing.
            function (Data, type) {
                // return the processed data
                return Data;
            }

15.global :
Requirements for the Boolean type parameter, the default is true. It indicates whether to trigger a global ajax event. Set to false will not trigger a global ajax event, ajaxStart or ajaxStop ajax be used to control a variety of events.

16.ifModified :
Requirements for the Boolean type parameter, the default is false. To obtain new data only when the server data changes. Server data change judgment based on the Last-Modified header information. The default value is false, ignoring the header information.

17.jsonp :
requirements parameter of type String, rewrite in the name of a callback function jsonp request. "? Callback =" This value is used to replace the GET or POST request in which the URL parameters in the "callback" section, e.g. {jsonp: 'onJsonPLoad'} will cause "onJsonPLoad =?" To the server.

18.username :
requirements for the parameters of type String, in response to a user name HTTP access authentication request.

19.password :
requirements for the parameters of type String, HTTP access authentication response to the password request.

20.processData :
Requirements for the Boolean type parameter, the default is true. By default, data transmission will be converted to an object (from a technical point of view is not a string) to match the default content type "application / x-www-form -urlencoded". If you want to send a DOM tree information or other undesirable information conversion, set to false.

21.scriptCharset :
requirements for the parameters of type String, only when the request is dataType "jsonp" or "script", and GET is only type for forcibly modifying the character set (charset). Not normally used in both local and remote content encoding.

These are my reference and reproduced in the content section

Then I will use the main part of the code below at the time of mapping

  $.ajax({
                url : "CourseServlet",
                async : true,
                type : "POST",
                data : {
                    "time" : time
                },
                dataType : "json",
                success : function(json) {
                    
                    
                    
                    for (var i = 0; i < json.length; i++) {
                        var d = {};
                        
                        d["name"] = json[i].Province;
                        d["value"] = json[i].Confirmed;
                        d["yisi"] = json[i].Yisi;
                        d["cured"] = json[i].Cured;
                        d["dead"] = json[i].Dead;
                        
                        
                        data.push(d);
                    }
                    error : function() {
                
                   alert("失败");
                  
               },

Mainly the time to send my servlet layer and then read the list to convert to json and chart

Reference to the original blog address:

Author: Tyler Ning 
Source: http://www.cnblogs.com/tylerdonet/ 
This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the apparent position of the original article page connection, if the problem through the following email address [email protected]   me, thank you very much linked.

Guess you like

Origin www.cnblogs.com/ljpljm/p/12445891.html