vue js ajax call无法读取属性' push'未定义的

时间:2017-03-28 13:05:35

标签: javascript php laravel-5.3 vuejs2 axios

在将数据从数据库推送到网页时,我遇到了vue.js的问题。我正在使用laravel 5.3将数据转换为Jason。代码向服务器发布要获取的记录数,然后是服务器响应,其中包含我想在网页上显示的数据。这是Vue js Code

new Vue({
        el: "#projects",
        data: {

            count: 0,
            fetched_projects: []
}

 methods:{
       checkproject: function(){
            var fetch = this.count += 4;               

           axios.post('/loadmore', {fetch: fetch })
               .then(function(response) {

                  this.fetched_projects.push(response.data);

               })
               .catch(function(error){
                   console.log(error);
               });

       }

HTML

 <div class="row">
    <div class="col-md-3">
        <ul v-for="more_projects in fetched_projects">
            <li>@{{ more_projects }}</li>

        </ul>

    </div>
</div>

Laravel控制器

 public function setloadMore(Request $request){

  $new_projects = $request->fetch;

  $results = Model1::with('relation')->take($new_projects)->get();

  return $results;


}

问题在于this.fetched_projects.push(response.data);它给了我&#34;无法读取属性&#39;推送&#39;未分解的&#34;

2 个答案:

答案 0 :(得分:2)

您希望 private class LongOperation extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.test.com/register.php"); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("name", params[0])); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } return ""; } @Override protected void onPostExecute(String result) { } @Override protected void onPreExecute() {} @Override protected void onProgressUpdate(Void... values) {} } 成为您的组件,因此请使用箭头功能:

new LongOperation().execute(userinputString1);

答案 1 :(得分:0)

你正在&#34;这个&#34;问题,尝试定义像var这样的东西,你已经定义了fetch,并在帖子中使用它代替它应该工作。