SyntaxError: Unexpected token o in JSON at position 1 when get rails json












0















Hi when i tried to search in my rails application (is not api mode) I work with rails 5 , webpacker and vue



in my controller this is my code



def search_information_sources
text = params[:text]
@list_books = Book.where('title ILIKE ?', "#{text}%").limit(10)
render json: @list_books
end


In my vue component i call the promise



    const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
axios.get("/syllabuses/search_information_sources", {
authenticity_token: token,
text: "negocios"
})
.then(function (response) {
console.log(response.data);
this.items_basic = JSON.parse(response.data);
})
.catch(function (error) {
alert(error);
toastr.error("No se pudo procesar la solicitud, " + error.response.data.message_error);
});


But when i execute this code i receive the error



This is my log in rails



Started GET "/syllabuses/search_information_sources.json" for ::1 at 2019-01-02 11:03:00 -0500
11:03:00 backend.1 | Processing by SyllabusesController#search_information_sources as JSON
11:03:00 backend.1 | User Load (3.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
11:03:00 backend.1 | ↳ app/controllers/application_controller.rb:32
11:03:00 backend.1 | Book Load (1.0ms) SELECT "books".* FROM "books" WHERE (title ILIKE '%') LIMIT $1 [["LIMIT", 10]]
11:03:00 backend.1 | ↳ app/controllers/syllabuses_controller.rb:143
11:03:00 backend.1 | Completed 200 OK in 48ms (Views: 16.8ms | ActiveRecord: 4.6ms)









share|improve this question




















  • 1





    where are you getting this error ?, is it happening in your controller ? or in your javascript code ?. It seems to be happening in your controller, if so, do you mind posting the Rails log of your request/response ?, are there any before_filters running in your controller ?.

    – fanta
    Jan 2 at 15:52











  • Hi fanta, i added my logs in my answer . The error i received when i print the error catch with alert(error) I dont have a before_filter for this action..

    – HalleyRios
    Jan 2 at 16:07













  • So it looks like your controller code is fine. Put a debugger in your then function before the console.log and see what you get there, try to execute the JSON.parse(response.data) line by hand and see if that causes the error.

    – fanta
    Jan 2 at 16:29











  • I think you don't have to use JSON.parse. from the axios docs: JSON responses are automatically parsed.

    – fanta
    Jan 2 at 16:58
















0















Hi when i tried to search in my rails application (is not api mode) I work with rails 5 , webpacker and vue



in my controller this is my code



def search_information_sources
text = params[:text]
@list_books = Book.where('title ILIKE ?', "#{text}%").limit(10)
render json: @list_books
end


In my vue component i call the promise



    const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
axios.get("/syllabuses/search_information_sources", {
authenticity_token: token,
text: "negocios"
})
.then(function (response) {
console.log(response.data);
this.items_basic = JSON.parse(response.data);
})
.catch(function (error) {
alert(error);
toastr.error("No se pudo procesar la solicitud, " + error.response.data.message_error);
});


But when i execute this code i receive the error



This is my log in rails



Started GET "/syllabuses/search_information_sources.json" for ::1 at 2019-01-02 11:03:00 -0500
11:03:00 backend.1 | Processing by SyllabusesController#search_information_sources as JSON
11:03:00 backend.1 | User Load (3.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
11:03:00 backend.1 | ↳ app/controllers/application_controller.rb:32
11:03:00 backend.1 | Book Load (1.0ms) SELECT "books".* FROM "books" WHERE (title ILIKE '%') LIMIT $1 [["LIMIT", 10]]
11:03:00 backend.1 | ↳ app/controllers/syllabuses_controller.rb:143
11:03:00 backend.1 | Completed 200 OK in 48ms (Views: 16.8ms | ActiveRecord: 4.6ms)









share|improve this question




















  • 1





    where are you getting this error ?, is it happening in your controller ? or in your javascript code ?. It seems to be happening in your controller, if so, do you mind posting the Rails log of your request/response ?, are there any before_filters running in your controller ?.

    – fanta
    Jan 2 at 15:52











  • Hi fanta, i added my logs in my answer . The error i received when i print the error catch with alert(error) I dont have a before_filter for this action..

    – HalleyRios
    Jan 2 at 16:07













  • So it looks like your controller code is fine. Put a debugger in your then function before the console.log and see what you get there, try to execute the JSON.parse(response.data) line by hand and see if that causes the error.

    – fanta
    Jan 2 at 16:29











  • I think you don't have to use JSON.parse. from the axios docs: JSON responses are automatically parsed.

    – fanta
    Jan 2 at 16:58














0












0








0








Hi when i tried to search in my rails application (is not api mode) I work with rails 5 , webpacker and vue



in my controller this is my code



def search_information_sources
text = params[:text]
@list_books = Book.where('title ILIKE ?', "#{text}%").limit(10)
render json: @list_books
end


In my vue component i call the promise



    const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
axios.get("/syllabuses/search_information_sources", {
authenticity_token: token,
text: "negocios"
})
.then(function (response) {
console.log(response.data);
this.items_basic = JSON.parse(response.data);
})
.catch(function (error) {
alert(error);
toastr.error("No se pudo procesar la solicitud, " + error.response.data.message_error);
});


But when i execute this code i receive the error



This is my log in rails



Started GET "/syllabuses/search_information_sources.json" for ::1 at 2019-01-02 11:03:00 -0500
11:03:00 backend.1 | Processing by SyllabusesController#search_information_sources as JSON
11:03:00 backend.1 | User Load (3.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
11:03:00 backend.1 | ↳ app/controllers/application_controller.rb:32
11:03:00 backend.1 | Book Load (1.0ms) SELECT "books".* FROM "books" WHERE (title ILIKE '%') LIMIT $1 [["LIMIT", 10]]
11:03:00 backend.1 | ↳ app/controllers/syllabuses_controller.rb:143
11:03:00 backend.1 | Completed 200 OK in 48ms (Views: 16.8ms | ActiveRecord: 4.6ms)









share|improve this question
















Hi when i tried to search in my rails application (is not api mode) I work with rails 5 , webpacker and vue



in my controller this is my code



def search_information_sources
text = params[:text]
@list_books = Book.where('title ILIKE ?', "#{text}%").limit(10)
render json: @list_books
end


In my vue component i call the promise



    const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
axios.get("/syllabuses/search_information_sources", {
authenticity_token: token,
text: "negocios"
})
.then(function (response) {
console.log(response.data);
this.items_basic = JSON.parse(response.data);
})
.catch(function (error) {
alert(error);
toastr.error("No se pudo procesar la solicitud, " + error.response.data.message_error);
});


But when i execute this code i receive the error



This is my log in rails



Started GET "/syllabuses/search_information_sources.json" for ::1 at 2019-01-02 11:03:00 -0500
11:03:00 backend.1 | Processing by SyllabusesController#search_information_sources as JSON
11:03:00 backend.1 | User Load (3.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
11:03:00 backend.1 | ↳ app/controllers/application_controller.rb:32
11:03:00 backend.1 | Book Load (1.0ms) SELECT "books".* FROM "books" WHERE (title ILIKE '%') LIMIT $1 [["LIMIT", 10]]
11:03:00 backend.1 | ↳ app/controllers/syllabuses_controller.rb:143
11:03:00 backend.1 | Completed 200 OK in 48ms (Views: 16.8ms | ActiveRecord: 4.6ms)






ruby-on-rails json webpack vuejs2 axios






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 16:06







HalleyRios

















asked Jan 2 at 15:44









HalleyRiosHalleyRios

899




899








  • 1





    where are you getting this error ?, is it happening in your controller ? or in your javascript code ?. It seems to be happening in your controller, if so, do you mind posting the Rails log of your request/response ?, are there any before_filters running in your controller ?.

    – fanta
    Jan 2 at 15:52











  • Hi fanta, i added my logs in my answer . The error i received when i print the error catch with alert(error) I dont have a before_filter for this action..

    – HalleyRios
    Jan 2 at 16:07













  • So it looks like your controller code is fine. Put a debugger in your then function before the console.log and see what you get there, try to execute the JSON.parse(response.data) line by hand and see if that causes the error.

    – fanta
    Jan 2 at 16:29











  • I think you don't have to use JSON.parse. from the axios docs: JSON responses are automatically parsed.

    – fanta
    Jan 2 at 16:58














  • 1





    where are you getting this error ?, is it happening in your controller ? or in your javascript code ?. It seems to be happening in your controller, if so, do you mind posting the Rails log of your request/response ?, are there any before_filters running in your controller ?.

    – fanta
    Jan 2 at 15:52











  • Hi fanta, i added my logs in my answer . The error i received when i print the error catch with alert(error) I dont have a before_filter for this action..

    – HalleyRios
    Jan 2 at 16:07













  • So it looks like your controller code is fine. Put a debugger in your then function before the console.log and see what you get there, try to execute the JSON.parse(response.data) line by hand and see if that causes the error.

    – fanta
    Jan 2 at 16:29











  • I think you don't have to use JSON.parse. from the axios docs: JSON responses are automatically parsed.

    – fanta
    Jan 2 at 16:58








1




1





where are you getting this error ?, is it happening in your controller ? or in your javascript code ?. It seems to be happening in your controller, if so, do you mind posting the Rails log of your request/response ?, are there any before_filters running in your controller ?.

– fanta
Jan 2 at 15:52





where are you getting this error ?, is it happening in your controller ? or in your javascript code ?. It seems to be happening in your controller, if so, do you mind posting the Rails log of your request/response ?, are there any before_filters running in your controller ?.

– fanta
Jan 2 at 15:52













Hi fanta, i added my logs in my answer . The error i received when i print the error catch with alert(error) I dont have a before_filter for this action..

– HalleyRios
Jan 2 at 16:07







Hi fanta, i added my logs in my answer . The error i received when i print the error catch with alert(error) I dont have a before_filter for this action..

– HalleyRios
Jan 2 at 16:07















So it looks like your controller code is fine. Put a debugger in your then function before the console.log and see what you get there, try to execute the JSON.parse(response.data) line by hand and see if that causes the error.

– fanta
Jan 2 at 16:29





So it looks like your controller code is fine. Put a debugger in your then function before the console.log and see what you get there, try to execute the JSON.parse(response.data) line by hand and see if that causes the error.

– fanta
Jan 2 at 16:29













I think you don't have to use JSON.parse. from the axios docs: JSON responses are automatically parsed.

– fanta
Jan 2 at 16:58





I think you don't have to use JSON.parse. from the axios docs: JSON responses are automatically parsed.

– fanta
Jan 2 at 16:58












1 Answer
1






active

oldest

votes


















0














I solution this problem, the problem is the context



var _that = this;


I use var _that = this; before call de promise and inside the then i use



_that.items_basic =response.data;


The final code for my javascript is



var _that = this;
const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
let resultado = ;
axios.get("/syllabuses/search_information_sources.json", {
authenticity_token: token,
params: {text_filter: text}
})
.then(function (response) {
console.log(response.data);
_that.items_basic = response.data;
})
.catch(function (error) {
alert(error);
toastr.error("No se pudo procesar la solicitud, " + error.response.data.message_error);
});





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009195%2fsyntaxerror-unexpected-token-o-in-json-at-position-1-when-get-rails-json%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I solution this problem, the problem is the context



    var _that = this;


    I use var _that = this; before call de promise and inside the then i use



    _that.items_basic =response.data;


    The final code for my javascript is



    var _that = this;
    const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
    let resultado = ;
    axios.get("/syllabuses/search_information_sources.json", {
    authenticity_token: token,
    params: {text_filter: text}
    })
    .then(function (response) {
    console.log(response.data);
    _that.items_basic = response.data;
    })
    .catch(function (error) {
    alert(error);
    toastr.error("No se pudo procesar la solicitud, " + error.response.data.message_error);
    });





    share|improve this answer




























      0














      I solution this problem, the problem is the context



      var _that = this;


      I use var _that = this; before call de promise and inside the then i use



      _that.items_basic =response.data;


      The final code for my javascript is



      var _that = this;
      const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
      let resultado = ;
      axios.get("/syllabuses/search_information_sources.json", {
      authenticity_token: token,
      params: {text_filter: text}
      })
      .then(function (response) {
      console.log(response.data);
      _that.items_basic = response.data;
      })
      .catch(function (error) {
      alert(error);
      toastr.error("No se pudo procesar la solicitud, " + error.response.data.message_error);
      });





      share|improve this answer


























        0












        0








        0







        I solution this problem, the problem is the context



        var _that = this;


        I use var _that = this; before call de promise and inside the then i use



        _that.items_basic =response.data;


        The final code for my javascript is



        var _that = this;
        const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        let resultado = ;
        axios.get("/syllabuses/search_information_sources.json", {
        authenticity_token: token,
        params: {text_filter: text}
        })
        .then(function (response) {
        console.log(response.data);
        _that.items_basic = response.data;
        })
        .catch(function (error) {
        alert(error);
        toastr.error("No se pudo procesar la solicitud, " + error.response.data.message_error);
        });





        share|improve this answer













        I solution this problem, the problem is the context



        var _that = this;


        I use var _that = this; before call de promise and inside the then i use



        _that.items_basic =response.data;


        The final code for my javascript is



        var _that = this;
        const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        let resultado = ;
        axios.get("/syllabuses/search_information_sources.json", {
        authenticity_token: token,
        params: {text_filter: text}
        })
        .then(function (response) {
        console.log(response.data);
        _that.items_basic = response.data;
        })
        .catch(function (error) {
        alert(error);
        toastr.error("No se pudo procesar la solicitud, " + error.response.data.message_error);
        });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 19:05









        HalleyRiosHalleyRios

        899




        899
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009195%2fsyntaxerror-unexpected-token-o-in-json-at-position-1-when-get-rails-json%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith