How to use jsonp in vue

How to use jsonp in vue

1. Introduction

Recently, I encountered a cross-domain problem when working on a handwriting input method. I used the interface of the QQ input method, and the proxy could not achieve the effect. Here I used jsonp to implement it. You can search Baidu for the principle of jsonp yourself. Here I will record the use of vue-jsonp and some small pitfalls. For the official documentation, please go to the npm address.

2. Installation

npm install vue-jsonp -S

or

yarn add vue-jsonp

3. Use

mian.js reference

// main.js
import Vue from 'vue'
import { VueJsonp } from 'vue-jsonp'
Vue.use(VueJsonp) // $jsonp is mounted on the vue prototype and can be used directly with vm.$jsonp()

Note: The package version here is 2.0.0. When importing, pay attention to using { } deconstruction assignment. Some tutorials on the Internet are old versions. If necessary, you can go to the official npm address in the preface for specific usage tutorials.

4. Use of vue files

this.$jsonp('/some-jsonp-url', {
  myCustomUrlParam: 'veryNice'
}).then(res => {
  // The code here is not executed // because the returned callback function will be called directly})

Assuming the name of the returned function is callbackFun, you need to bind the callbackFun function to the window object.

mounted() {
  // Bind the callbackFun method to the window object window['callbackFun'] = (data) => {
    cosole.log('defined callback function')
  }
  // Assume that callbackFun.ajax_callback() is returned
  window['callbackFun'] = {
    ajax_callback: function (res) {
      cosole.log('defined callback function')
    }
  }
}

Note: jsonp request method is only get

This is the end of this article about how to use jsonp in vue. For more information about the use of jsonp in vue, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to display Json format data in Vue
  • How to load local json data in vue
  • How to send Json format data in post request in Vue-cli
  • How to use simulated JSON data to view the effect in Vue
  • The two simplest ways to share Vue using JSON
  • How to get background json string in vue

<<:  Analyze the compilation and burning of Linux kernel and device tree

>>:  Example code for implementing transparent gradient effects with CSS

Recommend

Learn v-model and its modifiers in one article

Table of contents Preface Modifiers of v-model: l...

Detailed tutorial of pycharm and ssh remote access server docker

Background: Some experiments need to be completed...

Detailed explanation of Vue advanced construction properties

Table of contents 1. Directive custom directive 2...

HTML table markup tutorial (15): table title

<br />This tag can be used to directly add a...

Detailed explanation of the process of modifying Nginx files in centos7 docker

1. Install nginx in docker: It is very simple to ...

SQL Practice Exercise: Online Mall Database User Information Data Operation

Online shopping mall database-user information da...

Detailed explanation of .bash_profile file in Linux system

Table of contents 1. Environment variable $PATH: ...

How to run Hadoop and create images in Docker

Reinventing the wheel, here we use repackaging to...

Detailed explanation of nginx configuration file interpretation

The nginx configuration file is mainly divided in...

Basic understanding and use of HTML select option

Detailed explanation of HTML (select option) in ja...

Comprehensive understanding of html.css overflow

Comprehensive understanding of html.css overflow ...

Docker compose custom network to achieve fixed container IP address

Due to the default bridge network, the IP address...

A brief discussion on the semantics of HTML and some simple optimizations

1. What is semanticization? Explanation of Bing D...

Super detailed teaching on how to upgrade the version of MySQL

Table of contents 1. Introduction 2. Back up the ...