The difference between xml and json and cross-domain

 xml definition

Refers to Extensible Markup Language, used to transmit and store data. It is a markup language used to mark electronic files to make them structural. It can be used to mark data, define data types, and is a source language that allows users to define their own markup languages. XML uses DTD document type definition to organize data; the format is uniform, cross-platform and language, and has long been a recognized standard in the industry.

json definition                             

JSON is a lightweight data exchange format with good readability and quick writing features. Data exchange between different platforms is possible. json adopts a highly compatible, completely independent and language text format, and also has the habit of systems similar to c language\, java, python and so on. These make JSON an ideal data interchange language.

xml: Extensible Markup Language, a markup language used to mark electronic documents to make them structured.

json: (JavaScript Object Notation, JS Object Notation) is a lightweight data exchange format.

The difference between the two:

They are both a data interchange format.

1, xml is heavyweight, json is lightweight.

2. XML takes up more bandwidth during transmission, while json takes up less bandwidth and is easy to compress.

3. Both xml and json are used for project interaction, xml is mostly used for configuration files, and json is used for data interaction.

4. JSON can be parsed by jackson, gson and other methods, and xml can be parsed by dom, sax, demo4j and other methods.

 what is cross domain

Cross-domain: Refers to the fact that the browser cannot execute scripts from other websites. It is caused by the browser's same-origin policy, which is a security restriction imposed by the browser on javascript.

For example: page a wants to obtain resources of page b, if the protocols, domain names, ports, and subdomain names of pages a and b are different, the access actions performed are all cross-domain, and browsers generally restrict cross-domain access for security reasons , that is, cross-origin resource requests are not allowed. Note: Restricting access across domains is actually a browser restriction. It is important to understand this! ! !

Same-origin policy: It means that the protocol, domain name, and port must be the same, and if one of them is different, it will cause cross-domain;

The solution to cross-domain

The methods to solve cross-domain are:

1. Front-end framework: take vue as an example to modify vue.config.js

devServer: {
    open: true,
    host: 'localhost',
    port: 8080,
    overlay: {
        warnings: false,
        errors: true
    },
    proxy: {
        '/api': {
            target: '接口路径',
            ws: true,
            changOrigin: true,
            pathRewrite: {
            '^/api': ''
            }
        }
    }
}

2. JSONP

$.ajax({
      // ********************************基本写法够了
      // 内部实现过程就是上节优化写;不是XHR,造script标签;
      dataType: 'jsonp',
      url: "http://www.liulongbin.top:3006/api/jsonp",
      success: function(res) {
        console.log(res);
      },


      // ********************************灵活配置
      // 指定后面值:后台约定参数名
      // jsonp: 'callback',
      // 指定值:前端指定执行函数的名称
      // jsonpCallback: "fn"
    })

Guess you like

Origin blog.csdn.net/yjnain3066/article/details/129411099