开发者

Zend Framework: Inserting calls to View Helpers into Javascript code

开发者 https://www.devze.com 2023-04-06 23:13 出处:网络
I\'m developing a web application, where I use jQuery to make Ajax calls to the server. I\'m using the url view helper to insert the URL to be called. The code inside my view scripts looks like the f

I'm developing a web application, where I use jQuery to make Ajax calls to the server.

I'm using the url view helper to insert the URL to be called. The code inside my view scripts looks like the following:

$.ajax({
        type: "POST",
        url: "<?php echo $this->url(array开发者_StackOverflow中文版('action' => 'myaction', 'controller' => 'mycontroller'), null, true); ?>",
        dataType: "html",
        data: { id: myId, format: 'xml' },
        beforeSend: function() {
            // Show pseudoprogress
        },
        success: function(html) {
            // Process response
        },
        error: function() {
            // Show an error message
        }
    });

The system is working fine, but because of this approach I can't extract the Javascript code to separate files, because I always need to run it through the PHP interpreter first.

I wonder if there is a better way to do it, so that I can keep the Javascript code as "PHP-clean" as possible (and eventually, in separate files).

Thanks in advance.


Provide the url in a hidden input field or any other hidden markup element you like and get it from there with jQuery.

Example:

In the view:

<input type="hidden" id="myActionUrl" value="<?php echo $this->url(array('action' => 'myaction', 'controller' => 'mycontroller'), null, true); ?>" />

In the javascript:

url : $('#myActionUrl').val(), 


I would do this:

<script type="text/javascript">
   var url = "<?php echo $this->url(array('action' => 'myaction', 'controller' => 'mycontroller'), null, true); ?>";
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消