Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript symfony框架中的Ajax调用_Javascript_Php_Ajax_Symfony_Netbeans 7 - Fatal编程技术网

Javascript symfony框架中的Ajax调用

Javascript symfony框架中的Ajax调用,javascript,php,ajax,symfony,netbeans-7,Javascript,Php,Ajax,Symfony,Netbeans 7,我想用symfony2打ajax电话。我已经用平面php完成了ajax,我不知道如何在这个symfony框架中进行设置 <html> <head> <script> function showBook(str) { if (str == "") { document.getElementById("txtHint").innerHTML = ""; return;

我想用symfony2打ajax电话。我已经用平面php完成了ajax,我不知道如何在这个symfony框架中进行设置

    <html>
    <head>
    <script>
    function showBook(str) {
        if (str == "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else { 
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                }
            };
            xmlhttp.open("GET","getuser.php?q="+str,true);
            xmlhttp.send();
        }
    }
    function showAuthor(str){
        if (str == "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else { 
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                }
            };
            xmlhttp.open("GET","getAuthor.php?q="+str,true);
            xmlhttp.send();
        }
    }
    </script>
    </head>
    <body>

    <form action=""> 
    Book name: <input type="text" id="txt1" onkeyup="showBook(this.value)">
    <br><br>
    Author name:<input type="text" id="txt1" onkeyup="showAuthor(this.value)">
    </form>
    <br>
    <div id="txtHint"><b>book info will be listed here...</b></div>

    </body>
    </html>

功能展示手册(str){
如果(str==“”){
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}否则{
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
};
open(“GET”、“getuser.php?q=“+str,true”);
xmlhttp.send();
}
}
函数showAuthor(str){
如果(str==“”){
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}否则{
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
};
open(“GET”、“getAuthor.php?q=“+str,true”);
xmlhttp.send();
}
}
书名:


作者姓名:
图书信息将列在这里。。。
我应该在哪里传递此请求??到控制器?? 如何设置路线??
是否有任何方法可以使用平面php而不是控制器???

您可以将请求传递给使用路由公开的控制器操作:

然后,在html代码中,如果您正在使用twig并在脚本标记中包含javascript,则可以执行以下操作

xmlhttp.open("GET","{{ path("route_name", {"parameter_name":"parameter_value"}) }}");

如果要访问附加的.js文件中的路由,可以使用FOSJsRoutingBundle生成路由url

将请求传递给使用路由公开的控制器操作:

然后,在html代码中,如果您正在使用twig并在脚本标记中包含javascript,则可以执行以下操作

xmlhttp.open("GET","{{ path("route_name", {"parameter_name":"parameter_value"}) }}");

如果要访问附加的.js文件中的路由,可以使用FOSJsRoutingBundle生成路由url

如果您在表单中,可以执行以下操作:

    $(document).submit(function () {
        var url = $('form').attr('action');
        var data = $('form').serialize();

        $.post(url, data, function (data) {
            window.location.href = data.redirect;
        })
            .fail(function () {
                $('form').replaceWith(data.form);
            });
    });
您只需发送正确的url:

    $(document).on('click', 'a', function () {
            var url = window.location.href;

            $.get(url, function (data) {
                $('.container').replaceWith(data);
            });
    });
也可以使用路由生成,只需添加:
“friendsofsymfony/jsrouting bundle”:“dev master”
发送到composer.json

AppKernel.php:

新FOS\JsRoutingBundle\FOSJsRoutingBundle(

然后在routing.yml中进行配置:

fos_js_routing:
    resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"
最后在路由中使用“expose”arg:

@Route("/{table}/index", name="beta.index", options={"expose"=true})
我使用注释路由

在您的JS中:

var url = Routing.generate('beta.index', { 'table': 'foo' });

希望它能帮助您:)

如果您处于表单中,您可以执行以下操作:

    $(document).submit(function () {
        var url = $('form').attr('action');
        var data = $('form').serialize();

        $.post(url, data, function (data) {
            window.location.href = data.redirect;
        })
            .fail(function () {
                $('form').replaceWith(data.form);
            });
    });
您只需发送正确的url:

    $(document).on('click', 'a', function () {
            var url = window.location.href;

            $.get(url, function (data) {
                $('.container').replaceWith(data);
            });
    });
也可以使用路由生成,只需添加:
“friendsofsymfony/jsrouting bundle”:“dev master”
发送到composer.json

AppKernel.php:

新FOS\JsRoutingBundle\FOSJsRoutingBundle(

然后在routing.yml中进行配置:

fos_js_routing:
    resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"
最后在路由中使用“expose”arg:

@Route("/{table}/index", name="beta.index", options={"expose"=true})
我使用注释路由

在您的JS中:

var url = Routing.generate('beta.index', { 'table': 'foo' });
希望对您有所帮助:)

您可以阅读本教程:您可以阅读本教程: