简体   繁体   English

jQuery更改输入值后,Symfony2 Controller无法识别POST请求

[英]Symfony2 Controller does not recognize POST request after jquery changed input value

the following input field i have in my form: 我的表单中包含以下输入字段:

<input type="hidden" name="editShapeStatus" id="editShapeStatus" value="keep" />

the user can perform multiple actions which could change the value of the input. 用户可以执行可能会更改输入值的多个操作。

the jquery function to change the value: jquery函数更改值:

 $('#editShapeStatus').val("lto");

now it gets funny. 现在变得有趣了。 althought the form has method="POST" as an attribute, the server receives an GET request. 尽管该表单具有method =“ POST”作为属性,但是服务器接收到GET请求。 BUT only if the value of editShapeStatus was changed by jQuery. 仅当jQuery更改了editShapeStatus的值时,才可以。 An it gets better: 它会变得更好:

currently the value can be changed to "lto" and "uls". 当前值可以更改为“ lto”和“ uls”。 If i use firebug to change the status manually back to "keep", the server receives a normal POST request. 如果我使用Firebug将状态手动更改回“保持”状态,则服务器会收到正常的POST请求。 If i change the value to any of the other two manually, the server receives a GET request. 如果我将值手动更改为其他两个值中的任意一个,则服务器会收到GET请求。 Even changing the status names that can be passed to the input as the value will not work. 即使更改可以作为值传递给输入的状态名称,也将不起作用。 This only results in those two newly named status to cause the same error. 这只会导致这两个新命名的状态引起相同的错误。 interestingly enough, manually changing the value to any other string does work properly. 有趣的是,手动将值更改为任何其他字符串都可以正常工作。 even if i change the status names of "lto" and "uls", those two could be used, but not the new names. 即使我更改了状态名称“ lto”和“ uls”,也可以使用这两个名称,但不能使用新名称。

So i thought it must have something to do with jQuery. 所以我认为它一定与jQuery有关。 But the only way i am using it to alter the value i listed above. 但是,我使用它来更改上面列出的值的唯一方法。

any suggestions? 有什么建议么?

Edit: 编辑:

the form: 表格:

 <form role="form" method="POST" action="{{ path('save_border') }}">
            <div class="form-group">
                <input type="hidden" name="borderId" id="borderId" value="{{ border.id }}" /> {# here it tells the controller that we have an existing border to edit #}
                <input type="hidden" name="editShapeStatus" id="editShapeStatus" value="keep" />
                <input type="hidden" name="toLink" id="toLink" value="no" />
                <input type="hidden" name="shapeId" id="shapeId" value="{{ border.borderShape.id }}" />
            </div>
            <div class="form-group">
                <input class="form-control" type="hidden" name="vertices" id="vertices" required /> {# here the vertices need to be stored #}
            </div>
            <div class="form-group">
                <input class="form-control" type="text" name="countryName" id="countryName" placeholder="Germany" value="{{ border.name }}" required />
            </div>
            <div class="form-group">
                <div class="input-group">
                    <input class="form-control" type="number" name="year" id="year" placeholder=1868 value="{{ border.year }}" required />
                    <input type="hidden" name="adbcValueMeta" id="adbcValueMeta" value="{% if border.year < 0 %}bc{% else %}ad{% endif %}" />
                    <span class="input-group-btn">
                        <button type="button" class="btn btn-default" id="adbcSwitcherMeta">{% if border.year < 0 %}BC{% else %}AD{% endif %}</button>
                    </span>
                </div>
            </div>
            <div class="form-group">
                <textarea class="form-control" name="description" id="description" placeholder="Description">{{ border.description }}</textarea>
            </div>
            <div class="form-group">
                <button class="btn btn-success pull-right" type="submit" id="submitCountryBorderButton">
                    <span class="glyphicon glyphicon-ok-sign"></span> Save
                </button>
            </div>
        </form>

function to change the value of #editShapeStatus: 函数来更改#editShapeStatus的值:

$(document).on('click', '#useShapeForEditing', function() {
        $('#editShapeStatus').val("uls");
    });

the #userShapeForEditing button gets created dynamically. #userShapeForEditing按钮是动态创建的。

Symfony returned not quite accurate response when it said it could not find the GET Route. Symfony说无法找到GET Route,返回的响应不太准确。 There was acutally a call to a method of an entity that did not exist anymore. 从根本上来说,存在对不再存在的实体的方法的调用。 How Symfony managed to see that as a problem of routing does escape my knowledge, since the controller referenced by the route was indeed called. Symfony如何设法将其视为路由问题确实逃脱了我的知识,因为确实引用了路由引用的控制器。

The weird behaviour, which i thought was caused by jquery was actually just the result of the controller, who only executed that said non-existent method when the value in editShapeStatus was correct (if - else). 我认为这是由jquery引起的奇怪行为,实际上只是控制器的结果,当editShapeStatus中的值正确时(如果-else),控制器仅执行该不存在的方法。 thats why it worked with anything else. 这就是为什么它可以与其他任何东西一起使用的原因。

With the jsfiddle you provided, I clicked link and the value changed as you'll see below. 使用您提供的jsfiddle,我单击了link ,值更改了,如下所示。 Then I clicked save and a POST was made ... not a GET : 然后我单击“保存”,并进行了POST ...而不是GET

Remote Address:162.243.204.190:80
Request URL:http://fiddle.jshell.net/border/save/
Request Method:POST
Status Code:404 NOT FOUND
Request Headersview source
..........
Form Data:
borderId:12
editShapeStatus:uls
toLink:no
shapeId:38
vertices:
countryName:Poland
year:1868
adbcValueMeta:ad
description:blabla

And I also confirmed that a POST request is made when the value as is: keep . 我还确认了当值keep时发出了POST请求。

Therefore jQuery has nothing to do with the changing of the request method from POST to GET . 因此,jQuery与将请求方法从POST更改为GET无关。 We must look elsewhere! 我们必须在别处寻找!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM