Javascript 将修改后的json从视图发送到控制器

Javascript 将修改后的json从视图发送到控制器,javascript,jquery,ajax,symfony,symfony-3.4,Javascript,Jquery,Ajax,Symfony,Symfony 3.4,目前,我的视图中有一个可编辑的表,它是从一个带有javscript函数的json开始创建的。在这个表中,可以更改json的每个字段。我目前正在寻找一种方法,在单击按钮后传递这个用户编辑的json。问题是我从来没有做过Ajax调用 首先,这是我的观点: {% extends 'default/index.html.twig' %} {% block content %} <div class="json-view"> <!-- here is w

目前,我的视图中有一个可编辑的表,它是从一个带有javscript函数的json开始创建的。在这个表中,可以更改json的每个字段。我目前正在寻找一种方法,在单击按钮后传递这个用户编辑的json。问题是我从来没有做过Ajax调用

首先,这是我的观点:

{% extends 'default/index.html.twig' %} 
{% block content %}
    <div class="json-view">   <!-- here is where I print my table -->

    </div>
{% endblock %}
{% block javascripts %}
    // Here is where I execute all the function below
{% endblock %}
这是我用来创建可编辑表的javascript函数:

function jsonToTable(json, opts={}) {
  let headers = Object.keys(json[0]);
  let table = document.createElement('table');
  let thead = document.createElement('thead');
  let tbody = document.createElement('tbody');
  let thead_tr = document.createElement('tr');
  if (opts.class) table.classList.add(opts.class);
  headers.forEach(header => {
    let th = document.createElement('th');
    th.textContent = header;
    thead_tr.appendChild(th);
  });
  json.forEach(record => {
    let tr = document.createElement('tr');
    headers.forEach(field => {
      let td = document.createElement('td');
      td.textContent = record[field];
      td.setAttribute('contenteditable', true);
      tr.appendChild(td);
    });
    tbody.append(tr);
  });
  thead.appendChild(thead_tr);
  table.appendChild(thead);
  table.appendChild(tbody);
  return table;
}
这是我用来从表中取回编辑值的函数:

function tableToJson(table, options={}) {
  let fields = Array.from(table.querySelectorAll('thead tr th')).map(th => th.textContent);
  return Array.from(table.querySelectorAll('tbody tr')).map(tr => {
    return Array.from(tr.querySelectorAll('td')).reduce((record, td, index) => {
      return Object.assign(record, { [fields[index]] : formatValue(td.textContent) });
    }, {});
  });
}
我必须将表返回给控制器,因为我必须在后者中修改它并将其保存在数据库中

有什么方法可以做到这一点吗?

当然可以! 这是ajax调用:

$.ajax({
  url : "path('ajax_call_for_table')",
  type: "POST",
  data : table_data,
  success: function() { // some code on success }
});
/**
 * @Route("/ajax/table-data", name="ajax_call_for_table")
 * @Method("POST")
 */
public function setTableData(Request $request)
{
    if ($request->isXmlHttpRequest()) { // <- Check that is an ajax call
        $em = $this->getDoctrine()->getManager();

        $table_data = $request->request->get('table_data'); // <- Get the *data* of ajax

        /** Do something with this data */

        return new Response();
    }
}
其中,table_data是要修改的数据

然后,您必须在控制器中创建一个方法来处理此ajax调用:

$.ajax({
  url : "path('ajax_call_for_table')",
  type: "POST",
  data : table_data,
  success: function() { // some code on success }
});
/**
 * @Route("/ajax/table-data", name="ajax_call_for_table")
 * @Method("POST")
 */
public function setTableData(Request $request)
{
    if ($request->isXmlHttpRequest()) { // <- Check that is an ajax call
        $em = $this->getDoctrine()->getManager();

        $table_data = $request->request->get('table_data'); // <- Get the *data* of ajax

        /** Do something with this data */

        return new Response();
    }
}
/**
*@Route(“/ajax/table data”,name=“ajax\u call\u for\u table”)
*@Method(“POST”)
*/
公共函数setTableData(请求$Request)
{
如果($request->isXmlHttpRequest()){//getdoctor()->getManager();
$table_data=$request->request->get('table_data')//