php - recover or access data values in json array as string -


i have json response wich retuns me this:

[{"name":"value 1"}] 

in html.twig trying recover value like:

// note recover route parameter in javascript var slug , confchecked $.ajax({   type: "post",   url: routing.generate('get_product_by_all_config', {slug: $(this).val(selectedslug), confchecked: $(this).val(checkedconfigstring)} ),   datatype: 'json',   cache: false,   success: function(data) {     $('#target').empty();     $('#target').val(data.name);     $('#target').append(data.name);   } }); 

this symfony controller:

// recover $slug , $parameters requesr $request $entity = $this->getdoctrine()->getrepository('mybundle:entity')->getentitywithparameters($slug, $parameters);  $array = array(); $i = 0;  foreach($entity $e) {   $array[$i]['name'] = $e->getname();   break; }  $response = new response();  $data = json_encode($array);  $response->headers->set('content-type', 'application/json'); $response->setcontent($data);  return $response; 

i have no error, nothing happened, value not display in input id #target.

where wrong ?

edit

in controller made dump($response), returns me this:

response {#683 ▼   +headers: responseheaderbag {#696 ▶}   #content: "[{"name":"value 1"}]"   #version: "1.0"   #statuscode: 200   #statustext: "ok"   #charset: null } 

i think problem jquery/javascript return object var selectedslug , checkedconfigstring. there way make var values string php ?

in example json data array not object try : data[0].name instead of : data.name

are sure generated route

routing.generate('get_product_by_all_config', {slug: $(this).val(selectedslug), confchecked: $(this).val(checkedconfigstring)} ) 

is formed ?

$(this).val(selectedslug) , $(this).val(checkedconfigstring) seems weird me.

$(this).val(something) sets value , return array of object, not value.

routes can contains simple type such number or string. if want send complex data type (object), must serialize object , send in body of request (post only).

you can add property 'data' in ajax request containing object(s) :

$.ajax({     ...,     data : {         someprop : {property : 'value',...}     },     ... }); 

you can access these datas in symfony action's controller with

$this->get("request")->getcontent(); 

in case seems selectedslug , confchecked simple types may send them routes params : /myroute/my-slug/1 example, depending on routes configuration.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -