English 中文(简体)
如何将AJAX的论据传递到极端行动?
原标题:How to pass AJAX arguments to the extbase action?

现在,我设法从数据库中获取了数值, 我想具体说明更多我想要通过的东西。

从对下面的事件函数作出反应的选定框中, 我想读出一个值( 记录名称) 并将其传递到我的 ajax Action :


    
    var uid;
    $( #mySelectBox ).change(function() {
        arguments = $(this).attr( value );
        var uri =  <f:uri.action arguments="{uid:  +uid+ }" action="ajax" controller="Mycontroller1" pageType="89657201" /> ;

        jQuery.getJSON(uri, function(result) {
            // do something
        });
    });
    

此外,正如马库斯·比西奥罗夫(Marcus Biesioroff)建议的那样,我应该将我的联署材料另立一个文件,但随后我不得不自己而不是用流体方式写出义体,对吗?

我的ajaxAction看起来是这样的:


    
        public function ajaxAction($uid) {
            $dataFromRepo = $this->myRepository->findByUid($uid);

            $resultArray = array(
                "field1" => $dataFromRepo->getField1(),
                "field2" => $dataFromRepo->getField2(),
                "field3" => $dataFromRepo->getField3(),
                "field4" => $dataFromRepo->getField4(),
            );
            return json_encode($resultArray);
        }
    

我敢肯定,ud没有正确通过, 所有其他工作。

最佳回答

有一些错误:

  • You can t break vievhelper s syntax with JS even if it s placed in view, you need to pass full path of the action from <f:uri.action />
  • you cannot place this JS in view, because it contains curly brackets there s other description of the issue
  • you need to call ajax function from external file and pass to it action s link and uid separately, and then add the

认为:

<script type="text/javascript">
    var actionsPathFromViewHelperSetInTheView 
        =  <f:uri.action action="ajax" controller="Mycontroller1" pageType="89657201" /> ;
</script>
<script type="text/javascript" src="path/to/ext/Public/yourExternal.js"></script>


<!-- of course this field may/should be created with Fluid s viewhelper -->
<select id="mySelectBox" onchange="performAjaxCall(this)">
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
</select>

your external.js (当然您需要将 tx_ yourextkey_ your plugin 前缀更改为您自己的)

function performAjaxCall(selectFieldObj) {
    $.ajax({
        url: actionsPathFromViewHelperSetInTheView,
        data:{
            "tx_yourextkey_yourplugin[uid]":selectFieldObj.value
        },
        success:function (data) {
            // do something with your json
            alert( Load was performed. );
        }
    });
}

在您的控制器中 :

public function ajaxAction() {

    // try to always validate the incoming arguments
    if (!$this->request->hasArgument( uid ) || intval($this->request->getArgument( uid )) == 0) {
        header( HTTP/1.1 400 Bad Request );
        return json_encode(array( error =>  Bad request ));
    }

    $uid = intval($this->request->getArgument( uid ));

    $dataFromRepo = $this->myRepository->findByUid($uid);
    if ($dataFromRepo == null) {
        header( HTTP/1.1 404 Not found );
        return json_encode(
           array( error =>  Not found or you have no access or something else... happens... )
        );
    }
    $resultArray = array(
        "field1" => $dataFromRepo->getField1(),
        // etc...
    );

    return json_encode($resultArray);
}
问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签