First read a piece of code
The code is as follows: |
Copy code |
<Script type = "text/javascript"> JQuery (document). ready (function (){ JQuery (# my_input). change (function (){ Var value = jQuery (# my_input). val (); JQuery. ajax ({ Url: {path ('parteaccidentes _ ajax ', {'ergencia': value })}}, Timeout: 5000, Success: function (data ){ Alert ('OK '); }, Error: function (){ Alert ('mal '); } }); }); }); </Script> |
The address of this ajax request cannot be accessed normally.
In this code, the value of jQuery ("# my_input"). val () is assigned to the value, and you want to introduce the value variable
In the url address in ajax.
At this time, you will find that the value of the address you access is not introduced, but is processed as a string.
That is to say, the js value cannot be directly referenced in twig.
The reason is that twig parses php variables while value is js variables, so twig is considered a string by default.
Therefore, replace must be used for replacement.
The code is as follows. You can compare it with the above code:
The code is as follows: |
Copy code |
<Script type = "text/javascript"> JQuery (document). ready (function (){ JQuery (# my_input). change (function (){ Var value = jQuery (# my_input). val (); Var url = "{path ('parteaccidentes _ ajax ', {'ergencia': 'text '})}}"; Url = url. replace ("text", value ); JQuery. ajax ({ Url: url, Timeout: 5000, Success: function (data ){ Alert ('OK '); }, Error: function (){ Alert ('mal '); } }); }); }); </Script> |