4

Can anyone please help me with this error?

I have been using jQuery along with vue.js. Below is the method I am having issue with... It is called by a post request. new_user contains values received from the html form fields...

Values are being received nicely when I write it in console.

Problem comes when I try to use ajax method to store the data (this.$http.post()). I get the following error:

TypeError: Cannot read property 'replace' of undefined vue-resource.js:284

There were couple of links I went through.but i could take help from none... I might have made some silly mistakes because I used the above code from a perfectly running script. So if anyone can spot the mistake i would be grateful. Thanks... :)

Happy coding.

    addUser : function(new_user){
        // var instance = this;
        console.log(this)
        var new_user_input = this.new_user
        console.log(new_user_input.name)//this works fine..

         //problem comes right here
         this.$http.post("clients/" , new_user_input).then((response) => {
            alert("ok")
        },(response) => {
            alert("failed")
            this.form_errors = response.data
            // console.log("here should be the form errrors " + response.data.title)
        });
    }
10
  • The code looks ok, I would look the cause of your error in this.new_user data. What does it contain? Commented Oct 12, 2016 at 14:18
  • 1
    thanks for making the correction and a prompt reply. do you think using jQuery is causing this to happen since i am using $. the new_user is an object and contains data as per follows: new_user: Object address: "kathmandu" contact: "phone" dob: "1992" education: "bachelors" email: "[email protected]" gender: "male" name: "john doe" nationality: "Nepali" phone: "45678" Commented Oct 12, 2016 at 14:44
  • 1
    yeah that does make sense.. i donot really understand where i messed up then.. i am using vue v1.0.24 and vue resource v1.0.3 Commented Oct 12, 2016 at 14:51
  • 1
    Are you sure you want this.new_user instead of just new_user (that you're passing to the method). Commented Oct 12, 2016 at 17:11
  • 1
    Also try: this.$http({"clients/", method: 'POST', data: this.new_user}) Commented Dec 7, 2016 at 8:01

1 Answer 1

1

If your using Laravel, make sure that you have in you template:

Good:

<script>
        window.Laravel = <?php echo json_encode([
            'csrfToken' => csrf_token(),
        ]); ?>
</script>

//OR

<script>
    window.Laravel = {!! json_encode([
        'csrfToken' => csrf_token(),
    ]) !!}
</script>

BAD:

<script>
        window.Laravel = '<?php echo json_encode([
            'csrfToken' => csrf_token(),
        ]); ?>'
</script>

//OR

<script>
    window.Laravel = {{ json_encode([
        'csrfToken' => csrf_token(),
    ]) }}
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.