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

时间:2014-07-08 13:06:19

标签: php jquery symfony post get

我的表格中有以下输入字段:

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

用户可以执行多项操作,这些操作可能会改变输入的值。

更改值的jquery函数:

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

现在它变得有趣了。虽然表格有方法=&#34; POST&#34;作为属性,服务器接收GET请求。但是只有当jQuery改变了editShapeStatus的值时。它变得更好了:

目前该值可以更改为&#34; lto&#34;和&#34; uls&#34;。如果我使用firebug手动将状态更改回&#34; keep&#34;,则服务器会收到正常的POST请求。如果我手动将值更改为其他两个中的任何一个,则服务器会收到GET请求。甚至更改可以作为值传递给输入的状态名称也不起作用。这只会导致这两个新命名的状态导致相同的错误。有趣的是,手动将值更改为任何其他字符串确实可以正常工作。即使我改变了&#34; lto&#34;的状态名称。和&#34; uls&#34;,可以使用这两个,但不是新名称。

所以我认为它必须与jQuery有关。但我用它来改变我上面列出的价值的唯一方法。

有什么建议吗?

编辑:

形式:

 <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>

更改#editShapeStatus的值的函数:

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

#userShapeForEditing按钮是动态创建的。

2 个答案:

答案 0 :(得分:1)

当Symfony表示无法找到GET路线时,它的回复并不是很准确。实际上已经调用了一个不再存在的实体方法。 Symfony如何设法看到路由问题确实逃避了我的知识,因为路由引用的控制器确实被调用了。

我认为由jquery引起的奇怪行为实际上只是控制器的结果,当editShapeStatus中的值正确时(if - else),它只执行了所说的不存在的方法。这就是为什么它与其他任何东西一起工作。

答案 1 :(得分:0)

使用您提供的jsfiddle,我点击link并更改了值,如下所示。然后我点击了保存并制作了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

我还确认,当值为POST时,会发出keep个请求。

因此,jQuery与将请求方法从POST更改为GET无关。我们必须到别处寻找!