如何使用cakephp 2.x中的ajax发送参数并更新元素

时间:2021-11-06 13:46:48

I have an input field "#PostTitle" in which the user can enter a URL.

我有一个输入字段“#PostTitle”,用户可以在其中输入URL。

I want to send the user's input (upon change or upon exiting the field) to an action in my Posts controller which returns an array. Without CakePHP I think this would look something like this:

我想将用户的输入(在更改时或退出字段时)发送到我的Posts控制器中的一个动作,该动作返回一个数组。没有CakePHP我认为这看起来像这样:

$(document).ready(function(){
 $('#PostTitle').change(function(){
    $.ajax({
           type: 'POST',
           url: '/posts/setPostImages',  
           data: $(this).serialize(),
           success: function(data){
                do something;
           },
           error: function(message){
                console.debug(message);
           }
      });
 return false;
 });
});

The action in the Posts controller sets an array of links called $imageArray (I CURL the page and return an array of all the images on that page, if it's of any interest).

Posts控制器中的操作设置了一个名为$ imageArray的链接数组(I CURL页面,并返回该页面上所有图像的数组,如果感兴趣的话)。

Then, after I make the request, I would like to update an element which depends the contents of that array. The element contains the following code:

然后,在我发出请求之后,我想更新一个依赖于该数组内容的元素。该元素包含以下代码:

<div id="slider-wrap" class="boxframe">
    <div class="coda-slider" id="slider-id">
     <?php foreach ($imageArray as $image): ?>
        <div class="crop">
        <?php echo $this->Html->image($image); ?>
        </div>
    <?php endforeach; ?>
    </div>
</div>

Any help is appreciated!

任何帮助表示赞赏!

Edit: If I try this, the action isn't called at all:

编辑:如果我尝试这个,则根本不会调用该操作:

echo $this->Js->link('update',
      array('action' => 'setPostImages'),
      array(
         'update' => '#selectImage',
         'data' => 'www.*.com',
         'async' => true,    
         'dataExpression'=>true,
         'method' => 'POST'
      )
   );

1 个解决方案

#1


0  

If you want to generate this JS with CakePHP, take a look at JsHelper's event method and request method

如果要使用CakePHP生成此JS,请查看JsHelper的事件方法和请求方法

The action the POST request hits should render your element and respond with HTML. JsHelper::request will then replace the contents of the element specified by the update param with your HTML.

POST请求命中的操作应该呈现您的元素并使用HTML进行响应。然后,JsHelper :: request将用您的HTML替换update param指定的元素的内容。

Personally, I find it fairly obtuse to generate Javascript with a PHP framework. It quickly becomes difficult to do what you want. If you have even a moderate amount of Javascript, I recommend just writing it directly (you already seem to know how to do this!).

就个人而言,我发现使用PHP框架生成Javascript相当迟钝。很快就很难做到你想要的。如果你有相当数量的Javascript,我建议你直接写它(你似乎已经知道如何做到这一点!)。

Your edit refers to a possibly different issue. What kind of Javascript errors are you getting in your browser's console when you click the link generated by that snippet?

您的编辑指的是一个可能不同的问题。当您单击该代码段生成的链接时,您在浏览器的控制台中会出现什么样的Javascript错误?

#1


0  

If you want to generate this JS with CakePHP, take a look at JsHelper's event method and request method

如果要使用CakePHP生成此JS,请查看JsHelper的事件方法和请求方法

The action the POST request hits should render your element and respond with HTML. JsHelper::request will then replace the contents of the element specified by the update param with your HTML.

POST请求命中的操作应该呈现您的元素并使用HTML进行响应。然后,JsHelper :: request将用您的HTML替换update param指定的元素的内容。

Personally, I find it fairly obtuse to generate Javascript with a PHP framework. It quickly becomes difficult to do what you want. If you have even a moderate amount of Javascript, I recommend just writing it directly (you already seem to know how to do this!).

就个人而言,我发现使用PHP框架生成Javascript相当迟钝。很快就很难做到你想要的。如果你有相当数量的Javascript,我建议你直接写它(你似乎已经知道如何做到这一点!)。

Your edit refers to a possibly different issue. What kind of Javascript errors are you getting in your browser's console when you click the link generated by that snippet?

您的编辑指的是一个可能不同的问题。当您单击该代码段生成的链接时,您在浏览器的控制台中会出现什么样的Javascript错误?