Javascript 如何从Request::createFromGlobals()访问Symfony 3.4对象值

Javascript 如何从Request::createFromGlobals()访问Symfony 3.4对象值,javascript,php,symfony-3.4,Javascript,Php,Symfony 3.4,我试图在遗留项目中使用Symfony 3.4组件。有一个表单有两个字段:城市和州。我想将这两个字段发送到一个类方法。因此,我构建了一个中间PHP文件,用javascript将数据传递给用户。表单和javascript在这里 <form class="form-inline" id="addpharmacies"> <div class="form-group"> <label for="city" ><?php prin

我试图在遗留项目中使用Symfony 3.4组件。有一个表单有两个字段:城市和州。我想将这两个字段发送到一个类方法。因此,我构建了一个中间PHP文件,用javascript将数据传递给用户。表单和javascript在这里

     <form class="form-inline" id="addpharmacies">
    <div class="form-group">
        <label for="city" ><?php print xlt("City");?></label>
        <input type="text" class="form-control" id="city">
    </div>
    <div class="form-group">
        <label for="state"><?php print xlt("State");?></label>
        <input type="text" class="form-control" id="state">
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary" >Submit</button>
    </div>
</form>
}

正如您所看到的,我使用一个侦听器通过javascript函数importPharm提交表单。javascript通过对city和state字段的getElementById调用从表单获取数据

创建一个请求,将数据传递到该请求中,并调用助手文件来传递数据。我已验证正在将某些内容发送到帮助文件。但我在执行getContent()时可以看到的是[object]

当我尝试时:

    $request->request->get('city','');
我什么也得不到。我已经试过了我能在网上找到的每一种组合


如有任何建议,将不胜感激

您没有指定任何标题来澄清您的内容类型,因此Symfony检测您的内容类型并将其映射到(尽管它显示内容正文)是不明确的,请尝试添加:

Content-Type : application/x-www-form-urlencoded
标题并将表单输入作为表单数据发送,然后重试,否则需要内容体并将其作为数组或\StdClass对象处理

N.B:使用表单数据,您的请求如下所示

let body = "city="+city+"&state="+state;
let req = new Request('pharmacyHelper.php', {
    method: "POST",
    headers: {
        'Content-Type': 'application/x-www-form-url-encoded', 
        'Accept': 'application/json'
    },body: body});
fetch(req).then(response=> {
                    if (response.status === 200) {
                        return response.json()
                    } else {
                        throw new Error("Bad response from the server");
                    }
                })
                .then(json => {
                    alert(json) // Not a great UI experience
                })

谢谢你的回复。添加标题并没有解决问题。从主体中删除{}使主体只是文本而不是对象。这很好,因为我可以超越这一点。我测试了有无标题。然后,我测试了有无花括号。是{}在接收代码中创建了我无法解析的对象。
Content-Type : application/x-www-form-urlencoded
let body = "city="+city+"&state="+state;
let req = new Request('pharmacyHelper.php', {
    method: "POST",
    headers: {
        'Content-Type': 'application/x-www-form-url-encoded', 
        'Accept': 'application/json'
    },body: body});
fetch(req).then(response=> {
                    if (response.status === 200) {
                        return response.json()
                    } else {
                        throw new Error("Bad response from the server");
                    }
                })
                .then(json => {
                    alert(json) // Not a great UI experience
                })