【问题标题】:I can't using VUE js locally我无法在本地使用 VUE js
【发布时间】:2018-12-09 03:07:57
【问题描述】:

// 这是来自本地的它是 json 对象 { “身份证”:654321, “姓名”:“莫哈德”, “年龄”:22 }, { “身份证”:102030, “名称”:“法赫德”, “年龄”:18 } // 这是我的代码不工作

var app = new Vue({
  el: '#app',
  data: {
    socialId: '',
    person: ''
  },
  watch: {
    socialId: function() {
      this.person = ''
      if (this.socialId.length == 6) {
        this.lookupsocialId()
      }
    }
    },

  methods: {
    lookupsocialId: _.debounce(function() {
      var app = this
      app.person = "Searching..."
      axios.get('http://localhost:49999/api/people/' + app.socialId)
            .then(function (response) {
              app.person = response.data.Name + ', ' + response.data.Age
            })
            .catch(function (error) {
              app.person = "Invalid Zipcode"
            })
    }, 500)
  }
})

但是我用这个很好用
json对象{ “国家”:“美国”, “州”:“纽约”, “城市”:“SCHENECTADY” }

var app = new Vue({
  el: '#app',
  data: {
    socialId: '',
    person: ''
  },
  watch: {
    socialId: function() {
      this.person = ''
      if (this.socialId.length == 5) {
        this.lookupsocialId()
      }
    }
    },

  methods: {
    lookupsocialId: _.debounce(function() {
      var app = this
      app.person = "Searching..."
      axios.get('http://ziptasticapi.com/' + app.socialId)
            .then(function (response) {
              app.person = response.data.city
            })
            .catch(function (error) {
              app.person = "Invalid Zipcode"
            })
    }, 500)
  }
})

为什么当我在本地使用它时不起作用

【问题讨论】:

  • 澄清您的要求。如果您问为什么调用 localhost 端点不起作用,请调试您的本地端点。在进行 XHR 调用等之前对其进行单元测试。
  • 解释什么不起作用。

标签: c# vue.js


【解决方案1】:

你需要结果到json文件中,在你使用axio response.json()之后,你可以试试下面的代码

<script>
    export default{
        data(){
            return{
                title:"Form Register",
                formdata:{},
                message:"",
                success:0,
            }
        },
        methods:{
           register(){
                this.axios.post("http://localhost:8888/form-register",this.formdata).then((response) => {
                       console.log(response);
                       if(response.data.success>0){
                           this.message="You register success";
                           this.success=response.data.success;
                       }
                       else{
                           this.message="Register to failed";
                           this.success=response.data.success;
                       }
                  });
                    
            },
 
        }
    }
 
</script>
//construct json
[
  {
    name:"abc"
    age:34
  },
  {
    name:"abc"
    age:34
  }
]
if use php, you can echo json_encode() in php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 2022-07-17
    • 1970-01-01
    • 2018-08-20
    • 2020-03-31
    • 2021-01-07
    • 2021-07-04
    相关资源
    最近更新 更多