How can I set data's in twig from Ajax response

Maxim Strutinskiy


I can't set data's in twig from Ajax response. ${id}
I render content on front-end with jQuery template (use Ajax response), and I can't set data's in this template, twig give error:

Unexpected character "$".

Hear my JavaScript:

function ajaxSearchForm() {
var template =
        "<tr>" +
        "<td>${id}</td>" +
        "<td>${dpId}</td>" +
        "<td>${name}</td>" +
        "<td>${price}</td>" +
        "<td>${discount}</td>" +
        "<td>${description}</td>" +
        "<td>${image}</td>" +
        "<td>${companyName}</td>" +
        "<td>${categoryName}</td>" +
        "<td><a class='btn btn-sm btn-default edit_link'></a><i class='fa fa-cart-plus' aria-hidden='true'></i>" +
        "{{ render(controller('MainBundle:Certificate:addCertificate', {'certificate': " + ${id} ", 'cart': 1215'} )) }} " +
        "</td>" +
        "</tr>";

var empty =
        "<tr>" +
        "<th colspan='10'>No result...</th>" +
        "</tr>";

$('#form_search_certificate button').on('click', function (e) {
    e.preventDefault();

    $.ajax({
        type: "POST",
        url: "{{ path('app.admin.search.certificate')}}",
        dataType: 'json',
        data: $('#form_search_certificate').serialize(),
        success: function (data) {
            $("#search_result").empty();
            var dataParse = JSON.parse(data.result_search);
            if (dataParse.length !== 0) {
                for (var i = 0; i < dataParse.length; i++) {
                    $.tmpl(template, {
                        "id": dataParse[i].id,
                        "dpId": dataParse[i].dp_id,
                        "name": dataParse[i].name,
                        "price": dataParse[i].price,
                        "discount": dataParse[i].discount.value,
                        "description": dataParse[i].description,
                        "image": dataParse[i].image,
                        "companyName": dataParse[i].company.name,
                        "categoryName": dataParse[i].category.name,
                        "cart_id": data.cart_id
                    }).appendTo("#search_result");
                }
            } else {
                $.tmpl(empty, {}).appendTo("#search_result");
            }
        }
    });
});
}
 ajaxSearchForm();
Maxim Strutinskiy

I found the whey How set data in my ajax url:
In Action, wich rendering this template must have field cat_id (i get hem from return render()).
Then in ajax template i get this cat_id and set in url:

url: '{{ path("app.admin.search.certificate", {"cartId": cart_id}) }}',

And all is work !

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I repopulate the jquery UI datepicker from an Ajax response

From Dev

how can I parse ajax response in jquery?

From Dev

How to get the data response from ajax request?

From Dev

How can I set this value using ajax

From Dev

How can I populate a set of inputs with the data from the json object

From Dev

How can I get data from spring controller by ajax?

From Dev

How can I access the number of the dataProvider's current data set?

From Dev

How can I plot several charts from a set of sample data?

From Dev

How can I send compressed (gzip) JSON as response to an Ajax Request, from Java?

From Dev

How can I set the encoding of a httpExchange response?

From Dev

How can I disabled select option(s) base on the JSON data got from AJAX?

From Dev

How can i load data from ajax to Chosen jquery?

From Dev

Data From Ajax Response Can't Be Loaded In HighChart

From Dev

How can I loop through twig data

From Dev

Passing a JS var from AJAX response to Twig

From Dev

How can I set an HTML5 canvas ImageData from an XMLHttpRequest response?

From Dev

How to set AJAX response to jquery data table columns?

From Dev

How can I retrieve "data" from jQuery Ajax on server side?

From Dev

How to get the data response from ajax request?

From Dev

Can I get the Ajax json response data in selenium robot framework

From Dev

How can i extract data from response (socket)

From Dev

How can i remove particular data from JSON response

From Dev

How can I plot several charts from a set of sample data?

From Dev

How can i view Result of Database from doctrine in twig

From Dev

How can I disabled select option(s) base on the JSON data got from AJAX?

From Dev

using Python, Flask and Ajax, how can I parse a response from an ajax call and print as HTML?

From Dev

How can I set initial React state from API data?

From Dev

How can I get data from MySQL via Ajax?

From Dev

I cannot get the body data from JSONP's response with Angular2, but it works by using ajax

Related Related

  1. 1

    How can I repopulate the jquery UI datepicker from an Ajax response

  2. 2

    how can I parse ajax response in jquery?

  3. 3

    How to get the data response from ajax request?

  4. 4

    How can I set this value using ajax

  5. 5

    How can I populate a set of inputs with the data from the json object

  6. 6

    How can I get data from spring controller by ajax?

  7. 7

    How can I access the number of the dataProvider's current data set?

  8. 8

    How can I plot several charts from a set of sample data?

  9. 9

    How can I send compressed (gzip) JSON as response to an Ajax Request, from Java?

  10. 10

    How can I set the encoding of a httpExchange response?

  11. 11

    How can I disabled select option(s) base on the JSON data got from AJAX?

  12. 12

    How can i load data from ajax to Chosen jquery?

  13. 13

    Data From Ajax Response Can't Be Loaded In HighChart

  14. 14

    How can I loop through twig data

  15. 15

    Passing a JS var from AJAX response to Twig

  16. 16

    How can I set an HTML5 canvas ImageData from an XMLHttpRequest response?

  17. 17

    How to set AJAX response to jquery data table columns?

  18. 18

    How can I retrieve "data" from jQuery Ajax on server side?

  19. 19

    How to get the data response from ajax request?

  20. 20

    Can I get the Ajax json response data in selenium robot framework

  21. 21

    How can i extract data from response (socket)

  22. 22

    How can i remove particular data from JSON response

  23. 23

    How can I plot several charts from a set of sample data?

  24. 24

    How can i view Result of Database from doctrine in twig

  25. 25

    How can I disabled select option(s) base on the JSON data got from AJAX?

  26. 26

    using Python, Flask and Ajax, how can I parse a response from an ajax call and print as HTML?

  27. 27

    How can I set initial React state from API data?

  28. 28

    How can I get data from MySQL via Ajax?

  29. 29

    I cannot get the body data from JSONP's response with Angular2, but it works by using ajax

HotTag

Archive