标签: axios

Axios条纹问题。“ invalid_request_error”

我不知道该怎么做!我正在尝试通过Stripe API创建客户。用他们的例子卷曲我没有问题。这是他们的例子:

curl https://api.stripe.com/v1/customers \ -u sk_test_apikey: \ -d description="Customer for zoey.brown@example.com" \ -d source=tok_visa

当我尝试使用axios执行此操作时,出现错误“ invalid_request_error”,因为它无法正确解析我的数据。这是我所拥有的:

export const registerNewUser = async (firstName, lastName, email, password) => {
  let config = {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': `Bearer ${stripeTestApiKey}`
    }
  }
  let data = {
      email: `${email}`,
      description: `Customer account for ${email}`
  }
  await axios.post(stripeCustomerUri, data, config)
    .then(res => {
      console.log("DEBUG-axios.post--res: ", res)
    })
    .catch(err => {
      console.log(JSON.stringify(err, null, 2))
    })
}
Run Code Online (Sandbox Code Playgroud)

在我的控制台中,我看到条带没有以正确的方式接收我的数据。这是(我的有用部分)响应json:

"response": { 
  "data": { 
    "error": { 
      "type": …
Run Code Online (Sandbox Code Playgroud)

javascript stripe-payments axios

0
推荐指数
1
解决办法
758
查看次数

在axios中调用方法get forEach

我试图调用GetLikes(item.id)方法,它是在forEach和我的内axios.get功能。我得到一个错误提示TypeError: Cannot read property 'GetLikes' of undefined

如果我评论该方法,我可以看到我能够获取所有项目及其ID,但是当我取消对该方法的注释时,它将不再起作用。

axios
  .get("/api/endpoint")
  .then(response => {
    this.data = response.data;
    this.data.forEach(function(item) {
      console.log("found: ", item)
      console.log("found id: ", item.id)
      this.GetLikes(item.id);
    });
  })
Run Code Online (Sandbox Code Playgroud)

上面的代码输出: 似乎由于某种原因它无法获得ID 1,尽管没有下面的方法,相同的代码也获得ID 1。

found:  {…}
found id:  2
TypeError: Cannot read property 'GetLikes' of undefined
Run Code Online (Sandbox Code Playgroud)

注释掉this.GetLikes(item.id)的输出:

found:  {…}
found id:  2
found:  {…}
found id:  1
Run Code Online (Sandbox Code Playgroud)

^以上显然可以获取所有项目,因此,如果我尝试对这些项目调用方法,为什么会得到未定义的信息?

下面的代码有效(获得正确的赞)。我在用户按下“赞”按钮时使用它,但是我首先还需要获得所有赞,这就是我上面想要做的。

Like(id) {
  axios
    .post("/like/" + id)
    .then(response => {
      this.GetLikes(id);
    })
}
Run Code Online (Sandbox Code Playgroud)

我在这里想念什么?

javascript vue.js axios

0
推荐指数
1
解决办法
6105
查看次数

如何在React中使用axios删除单个项目

我已经研究了许多类似这样的文章和帖子,但在我的情况下不起作用,我只需要使用axios从我的应用程序中的帖子列表中删除一个项目即可。在axios文档中,它说您需要将参数传递给delete方法。另外,在大多数应用程序中,我都使用过ID,而ID却没有处于状态。但是我不能让它工作。请查看我的整个代码。我知道我的删除方法有误,请帮助我修复它:

    // The individual post component
    const Post = props => (
    <article className="post">
        <h2 className="post-title">{props.title}</h2>
        <hr />
        <p className="post-content">{props.content}</p>
        <button onClick={props.delete}>Delete this post</button>
    </article>
);

// The seperate form component to be written later

class Form extends React.Component {}

// The posts loop component

class Posts extends React.Component {
    state = {
        posts: [],
        post: {
            title: "",
            content: ""
        }
        // error:false
    };

    componentDidMount() {
        const { posts } = this.state;
        axios
            .get("url")
            .then(response => { …
Run Code Online (Sandbox Code Playgroud)

firebase reactjs axios

0
推荐指数
1
解决办法
4486
查看次数

JS Axios-发生错误时如何获取响应正文?

我希望能够从Axios错误捕获中获取响应正文。

我正在使用axios v0.18.0。

我的axios代码如下所示:

let service = axios.create({
                baseURL: "https://baseUrl.azurewebsites.net",
                responseType: "json"
            });

        service.defaults.headers.common['authorization'] = `Bearer ${token}`;

        service.post("/api/method", params).then(result => {
            console.log('success',result);
        }).catch(error=>{
            console.log('error');
            console.log(error);
        });
Run Code Online (Sandbox Code Playgroud)

给定输入,我的API调用返回了我期望的400错误。因此,我遇到了麻烦。但是,我无法检索API调用返回的错误消息。

我尝试做一个console.out(error.response.data),但这返回null。

我已经使用Postman进行了验证,该API调用的确会在响应正文返回错误消息,因此API并不是问题。

我想念什么?

javascript error-handling axios

0
推荐指数
1
解决办法
2838
查看次数

将邮递员请求模拟到Axios?

在此处输入图片说明

在此处输入图片说明

我正在尝试构造自己axios的邮件,以模仿邮递员的请求,但是失败了。请帮忙看看

const ax = axios.create({
  timeout: 30000,
  headers: {
    'content-type': 'application/x-www-form-urlencoded'
  }
});

// Attempt register operation
ax.post('https://url/5bc9ff9628d79b6d274165da/update.json', {
  body: JSON.stringify({
    json: JSON.stringify({ "stat": "Delivered" })
  })
})
.then(({ data }) => {
  console.log('DEBUG::success!!! data::', data);
})
.catch((err) => {
  console.log('DEBUG::err', err);
});  
Run Code Online (Sandbox Code Playgroud)

api reactjs postman react-native axios

0
推荐指数
2
解决办法
1132
查看次数

如何使用axios设置标题

我试图从我发现的API足球中获取信息。在邮递员上,它运作良好,但我无法使用axios正确设置标题。

这是我的代码:

class App extends React.Component {
  constructor(props) {
    super(props);
  }

  getTest = (e) => {
    e.preventDefault();
    let headers = {
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '*',
      'X-Auth-Token': '97e0d315477f435489cf04904c9d0e6co',
    };

    axios.get("https://api.football-data.org/v2/teams/90/matches?status=SCHEDULED", {headers})
      .then(res => {
        console.log("DATA")
      })
      .catch(res => {
        console.log("ERR", res);
      });
  }

    render() {
    return (
      <div>
        <button onClick={this.getTest}>info</button>
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

问题是什么 ?谢谢

api reactjs axios

0
推荐指数
1
解决办法
8556
查看次数

如何忽略节点请求中的SSL证书验证?

我需要https使用node.js 对某些请求禁用对等SSL验证node-fetch,据我所知,现在我使用的包没有该选项。

那应该是CURL的 CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false

是否有任何联网软件包都允许这样做?有没有一种方法可以跳过axios中的SSL验证?

node.js axios node-fetch

0
推荐指数
1
解决办法
3191
查看次数

将Webpack与Vue结合使用时,如何解决“无法读取未定义的属性'_wrapper'”错误?

我使用axios从数据库获取信息。我从阵列中的服务器得到响应:

响应

我的html标签: htmlTag

Js代码如下所示:

    data() {
        return {
            departments: {
                id: 0,
                name: "name of company",
                nameZod: "name of company dif"
                },
            };
       },
    created() {
        axios
            .get('/SomeController/Departments')
            .then(response => {
                this.departments = response.data;
                console.log(this.departments);
            })
        }
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:无法读取未定义的属性'_wrapper'。这是非常奇怪的原因,因为我在其他模块中使用了类似的代码,并且可以正常工作。

webpack vue.js axios

0
推荐指数
2
解决办法
1922
查看次数

在Axios通话之外无法获得价值

我希望您能解决axios遇到的问题。

我只是想在通话之外获得价值,但无法发挥作用。这是一段小代码:

 axios.get('/api/get-user').then(({ data }) => {
            this.user = data;
            console.log(this.user); //returns the correct data
            return this.user;
        });

        console.log(this.user); //returns null
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?我也尝试过,let self = this但无济于事。我希望你们能帮助我!

javascript vue.js axios

0
推荐指数
1
解决办法
45
查看次数

从axios get和post返回的数据/承诺是否有所不同?

我正在开发一个React应用程序,该应用程序使用导入的对象以及对api的获取请求和对相关API的发布请求。

在React的前端中创建服务的新实例时,我能够成功地使用'.then'和'.catch'函数来仅从get请求访问返回的数据。

当使用来自同一对象的发布请求时,尝试访问响应对象时,我得到了(反义)“。then”,它不是未定义的函数。

只有当我在表单提交功能中显式写出发布请求(不使用服务)并处理对象时,我才能检查响应并随后设置状态。

在React中使用axios的适当/最佳实践方法是什么?为什么在创建服务的新实例时不能访问响应对象?非常感激!

服务:

import axios from 'axios';

class ProductServices {
  getAllProducts(){
    return axios.get('https://somecustomAPIURL')
  }

  postProduct(somePathConfig){
    axios.request({
      url: 'https://somecustomAPIURL' + somePathConfig,
      method: 'post',
      headers: {'some-custom-header': process.env.REACT_APP_API_POST_KEY}
    })
  }

}

export default ProductServices;
Run Code Online (Sandbox Code Playgroud)
React Code instantiating and consuming the service (note, that getAllProducts works just fine, but trying to consume a response object in postProduct returns an '.then' is undefined)


  constructor(){
    super();
    this.state = {
      products: [],
      productID: null,
      showModal: false
    }
    this.ProductServices = new ProductServices();
  }

  getAllProducts = …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs axios

0
推荐指数
1
解决办法
51
查看次数