首页 文章

如何从非Symfony2站点向Symfony2提交表单

提问于
浏览
0

我正在构建一个移动Web应用程序,它从我构建的Symfony2应用程序中获取信息 . 用户必须提交一个表单才能查看信息,但是当我通过AJAX提交表单时,处理它的控制器看不到提交的表单 .

移动应用程序是使用Cordova构建的,因此表单源自静态HTML表单 . 一旦通过AJAX提交,然后将返回所需的数据 . 但是,表单提交了精确的触发AJAX来查看正在提交的表单's magic, but Symfony2 doesn't . 我在 $request = $this->getRequest(); 上做了一个 var_dump ,这里不包含表格 . 这是转储:

object(Symfony \ Component \ HttpFoundation \ Request)#6(20){[“attributes”] => object(Symfony \ Component \ HttpFoundation \ ParameterBag)#9(1){[“parameters”:protected] => array (3){[“_ conttroller”] => string(60)“CWWA \ CoreBundle \ Controller \ AjaxController :: getscheduleAction”[“_route”] => string(17)“ajax_get_schedule”[“_route_params”] => array( 0){}}} [“request”] => object(Symfony \ Component \ HttpFoundation \ ParameterBag)#7(1){[“parameters”:protected] => array(0){}} [“query”] => object(Symfony \ Component \ HttpFoundation \ ParameterBag)#8(1){[“parameters”:protected] => array(0){}} [“server”] => object(Symfony \ Component \ HttpFoundation \ ServerBag )#12(1){[“parameters”:protected] => array(35){[“BASE”] => string(9)“/ cwwa / web”[“CONTENT_LENGTH”] => string(2)“ 70“[”CONTENT_TYPE“] => string(31)”application / json; charset = UTF-8“[”DOCUMENT_ROOT“] => string(8)”/ var / www“[”GATEWAY_INTERFACE“] => string( 7)“CGI / 1.1”[“HTTP_ACCEPT”] => string(46)“application / json,text / javascript,/; q = 0.01”[“HTTP_ACC EPT_ENCODING“] => string(13)”gzip,deflate“[”HTTP_ACCEPT_LANGUAGE“] => string(14)”en-gb,en; q = 0.5“[”HTTP_CACHE_CONTROL“] => string(8)”no- cache“[”HTTP_CONNECTION“] => string(10)”keep-alive“[”HTTP_COOKIE“] => string(36)”PHPSESSID = lvfqh6hcdampcah8mi93il6922“[”HTTP_HOST“] => string(9)”domain.com“ [“HTTP_PRAGMA”] => string(8)“no-cache”[“HTTP_REFERER”] => string(26)“http://domain.com/cwwa-app/”[“HTTP_USER_AGENT”] => string( 85)“Mozilla / 5.0(X11; Linux x86_64; rv:24.0)Gecko / 20140722 Firefox / 24.0 Iceweasel / 24.7.0“[”PATH“] => string(13)”/ bin:/ usr / bin“[”PATH_INFO“] => string(22)”/ ajax / client / schedule /“[”PATH_TRANSLATED“] => string(29)”/ var / www / wwwa /web / app_dev.php“[”QUERY_STRING“] => string(0)”“[”REDIRECT_STATUS“] = > string(3)“200”[“REMOTE_ADDR”] => string(3)“:: 1”[“REMOTE_PORT”] => string(5)“33568”[“REQUEST_METHOD”] => string(4)“ POST“[”REQUEST_URI“] => string(43)”/cwwa /web/app_dev.php/ajax/client/schedule/“[”SCRIPT_FILENAME“] => string(29)”/ var / www / cwwa / web /app_dev.php“[”SCRIPT_NAME“] => string(21)”/ cwwa /web / app_dev.php“[”SERVER_ADDR“] => string(3)”:: 1“[”SERVER_ADMIN“] => string (19)“webmaster @ localhost”[“SERVER_NAME”] => string(9)“localhost”[“SERVER_PORT”] => string(2)“80”[“SERVER_PROTOCOL”] => string(8)“HTTP / 1.1“[”SERVER_SIGNATURE“] => string(70)”localhost端口80上的Apache / 2.2.22(Debian)服务器“[”SERVER_SOFTWARE“] => string(22)”Apache / 2.2.22(Debian)“[ “PHP_SELF”] => string(43)“/cwwa /web/app_dev.php/ajax/client/schedule/ “[”REQUEST_TIME“] => int(1409142446)}} [”files“] => object(Symfony \ Component \ HttpFoundation \ FileBag)#11(1){[”parameters“:protected] => array(0) {}} [“cookies”] => object(Symfony \ Component \ HttpFoundation \ ParameterBag)#10(1){[“parameters”:protected] => array(1){[“PHPSESSID”] => string(26 )“lvfqh6hcdampcah8mi93il6922”}} [“headers”] => object(Symfony \ Component \ HttpFoundation \ HeaderBag)#13(2){[“headers”:protected] => array(13){[“content-length”] => array(1){[0] => string(2)“70”} [“content-type”] => array(1){[0] => string(31)“application / json; charset = UTF-8“} [”accept“] => array(1){[0] => string(46)”application / json,text / javascript,/; q = 0.01“} [”accept-encoding“] => array(1){[0] => string(13)”gzip,deflate“} [”accept-language“] => array(1){[0 ] => string(14)“en-gb,en; q = 0.5”} [“cache-control”] => array(1){[0] => string(8)“no-cache”} [“ connection“] => array(1){[0] => string(10)”keep-alive“} [”cookie“] => array(1){[0] => string(36)”PHPSESSID = lvfqh6hcdampcah8mi93il6922 “} [”host“] => array(1){[0] => string(9)”localhost“} [”pragma“] => array(1){[0] => string(8)”no -cache“} [”referer“] => array(1){[0] => string(26)”http://domain.com/cwwa-app/“} [”user-agent“] => array (1){[0] => string(85)“Mozilla / 5.0(X11; Linux x86_64; rv:24.0)Gecko / 20140722 Firefox / 24.0 Iceweasel / 24.7.0”} [“x-php-ob-level” ] =>array(1){[0] => int(1)}} [“cacheControl”:protected] => array(1){[“no-cache”] => bool(true)}} [“content”: protected] => NULL [“languages”:protected] => NULL [“charsets”:protected] => NULL [“acceptableContentTypes”:protected] => NULL [“pathInfo”:protected] => string(22)“/ ajax / client / schedule /“[”requestUri“:protected] => string(43)”/cwwa /web/app_dev.php/ajax/client/schedule/“[”baseUrl“:protected] => string(21) “/cwwa/web/app_dev.php”[“basePath”:protected] => NULL [“method”:protected] => string(4)“POST”[“format”:protected] => NULL [“session” :protected] => object(Symfony \ Component \ HttpFoundation \ Session \ Session)#67(3){[“storage”:protected] => object(Symfony \ Component \ HttpFoundation \ Session \ Storage \ NativeSessionStorage)#66(5 ){[“bags”:protected] => array(2){[“attributes”] => object(Symfony \ Component \ HttpFoundation \ Session \ Attribute \ AttributeBag)#62(3){[“name”:“Symfony \ Component \ HttpFoundation \ Session \ Attribute \ AttributeBag“:private] => string(10)”attributes“[”storageKey“:”Symfony \ Com ponent \ HttpFoundation \ Session \ Attribute \ AttributeBag“:private] => string(15)”_ sf2_attributes“[”attributes“:protected] => array(0){}} [”flashes“] => object(Symfony \ Component) \ HttpFoundation \ Session \ Flash \ FlashBag)#61(3){[“name”:“Symfony \ Component \ HttpFoundation \ Session \ Flash \ FlashBag”:private] => string(7)“闪烁”[“闪烁”: “Symfony \ Component \ HttpFoundation \ Session \ Flash \ FlashBag”:private] => array(0){} [“storageKey”:“Symfony \ Component \ HttpFoundation \ Session \ Flash \ FlashBag”:private] => string(12 )“sf2_flashes”}} [“started”:protected] => bool(false)[“closed”:protected] => bool(false)[“saveHandler”:protected] => object(Symfony \ Component \ HttpFoundation \ Session \ Storage \ Proxy \ NativeProxy)#63(3){[“wrapper”:protected] => bool(false)[“active”:protected] => bool(false)[“saveHandlerName”:protected] => string( 5)“files”} [“metadataBag”:protected] => object(Symfony \ Component \ HttpFoundation \ Session \ Storage \ MetadataBag)#64(4){[“name”:“Symfony \ Component \ HttpFoundation \ Session \ Storage \元dataBag“:private] => string(10)”_ metadata“[”storageKey“:”Symfony \ Component \ HttpFoundation \ Session \ Storage \ MetadataBag“:private] => string(9)”_ sf2_meta“[”meta“:protected ] => array(3){[“c”] => int(0)[“u”] => int(0)[“l”] => int(0)} [“lastUsed”:“Symfony \ Component \ HttpFoundation \ Session \ Storage \ MetadataBag“:private] => NULL}} [”flashName“:”Symfony \ Component \ HttpFoundation \ Session \ Session“:private] => string(7)”flashes“[”attributeName“ :“Symfony \ Component \ HttpFoundation \ Session \ Session”:private] => string(10)“attributes”} [“locale”:protected] => NULL [“defaultLocale”:protected] => string(2)“en “}

HTML表单是这样的:

<form method="post" name="scf" id="scheduleChangeForm" action="http://localhost/cwwa/web/app_dev.php/ajax/client/schedule/">
            <input type="hidden" id="scheduleChangeCustomer" class="form-control" name="scf[customer]" />
            <select id="divisionSelect" class="form-control" name="scf[division]">
                <option>All Divisions</option>
            </select>
            <select id="siteSelect" class="form-control siteDivSelect" name="scf[site]">
                <option>All Sites</option>
            </select>
            <button type="submit" class="btn btn-primary btn-go" id="chSBtn">Submit <i class="fa fa-arrow-circle-right"></i></button>
        </form>

AJAX是这样的:

var scf = $('#scheduleChangeForm');

scf.submit(function(){

    $.ajax({
        type: scf.attr('method'),
        url: scf.attr('action'),
        dataType: 'json',
        crossDomain: true,
        contentType: 'application/json',
        data: scf.serialize(),
        success: function(data) {
            // Deals with a successful AJAX submit
        }
    });
});

EDIT
我已将以下代码添加到控制器:

$content = $this->get("request")->getContent();
print_r($content);

这返回了以下内容:

scf%5Bcustomer%5D = 1&scf%5Bdivision%5D = Circom Managed&scf%5Bsite%5D = 633

因此表单正在提交给请求,但不确定为什么它会像这样显示 .

1 回答

  • 0

    我想你可能需要一个csrf令牌?

相关问题