Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在Ubuntu上,Taro.request()请求json数据发送后,没有执行success()回调。 #3095

Closed
numeny opened this issue May 19, 2019 · 3 comments
Assignees

Comments

@numeny
Copy link

numeny commented May 19, 2019

问题描述
在Ubuntu上,Taro.request()请求json数据发送后,没有执行success()回调。

复现步骤

  1. 用taro init myApp新建一个项目。
  2. 建立一个Button,并且关联click_button
  3. 在click_button函数里,调用Toast.request()
  4. 点击Button,弹出Toast,显示fail.
  5. 用浏览器访问http://localhost/t.json, 显示
    { name: "a", OK : "true" }
// ```js
import Taro, { Component, Config } from '@tarojs/taro'
import { View, Text, Button } from '@tarojs/components'
import './index.scss'

export default class Index extends Component {

  /**
   * 指定config的类型声明为: Taro.Config
   *
   * 由于 typescript 对于 object 类型推导只能推出 Key 的基本类型
   * 对于像 navigationBarTextStyle: 'black' 这样的推导出的类型是 string
   * 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型
   */
  config: Config = {
    navigationBarTitleText: '首页'
  }

  componentWillMount () { }

  componentDidMount () { }

  click_button (e) {
    Taro.request({
      url: 'http://localhost/t.json',
      dataType: 'json',
      success: (res) => {
        Taro.showToast({title: 'success'})
      },
      fail: (error) => {
        Taro.showToast({title: 'fail'})
      },
      complete: () => {
        // Taro.showToast({title: "complete"})
      },
    })
  }

  componentWillUnmount () { }

  componentDidShow () { }

  componentDidHide () { }

  render () {
    return (
      <View className='index'>
        <Text>Hello world!</Text>
        <Button size='mini' type='warn' onClick={this.click_button.bind(this)}>按钮</Button>
      </View>
    )
  }
}
// ```

期望行为
期望能够Toast.request()能够成功,回调success() 函数,并且获得数据,弹出Toast,显示"success"

报错信息

  1. 第一种场景(即复现步骤里描述场景和代码):这种场景下有chrome的network调试窗口上有json数据返回
    Failed to load http://localhost/t.json: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:10086' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
    (index):1
    Uncaught (in promise) TypeError: Failed to fetch
    case1_1
    case1_2

  2. 根据上一种场景错误提示,当增加Taro.request()参数mode: 'no-cors',时报错如下:
    index.js?3a5f:83 Uncaught (in promise) SyntaxError: Unexpected end of input
    at eval (index.js?3a5f:83)
    并且,依然弹出错误Toast。这种场景下有chrome的network调试窗口上有json数据返回
    case2_1
    case2_2
    case2_3
    case2_4

  3. 后来我又在场景二基础上,修改dataType: 'html',这次弹出Toast,显示success(回调了success()函数,但是返回的data却是NULL。console上没有任何错误信息提示。
    case3_1

系统信息
Taro v1.3.0-beta.3

Taro CLI 1.3.0-beta.3 environment info:
System:
OS: Linux 4.13 Ubuntu 17.10 (Artful Aardvark)
Shell: 4.4.12 - /bin/bash
Binaries:
Node: 10.15.3 - ~/.nvm/versions/node/v10.15.3/bin/node
npm: 6.4.1 - ~/.nvm/versions/node/v10.15.3/bin/npm
报错平台:h5

补充信息
场景二应该是OK的场景,但是不知道为什么显示SyntaxError

@taro-bot
Copy link

taro-bot bot commented May 19, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@taro-bot
Copy link

taro-bot bot commented Nov 18, 2019

CC @Littly

@ZakaryCode
Copy link
Contributor

这只是个跨域问题,和 Taro 无关,可以参考 MDN 的相关文档

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants