ajax 请求需要协助

Posted

技术标签:

【中文标题】ajax 请求需要协助【英文标题】:Assitance required with ajax request 【发布时间】:2013-05-27 11:13:21 【问题描述】:

您好,我在 Eclipse 中使用 Play framework 1.2.3,我想用这个 java 请求调用控制器方法,但是当我单击按钮时没有任何反应。有谁知道出了什么问题?

最好的问候。

<script type="text/javascript">   
    $("#request").click(function()  

            var reqphone = $('#reqPhone').val();  
            var key = $('#accesscode).val();    
            var fro = $('#from').val();  
            var too = $('#to').val();  
            var myPhone = $('#userPhone').val();  

            var action = #jsAction @reversetest(':consumer',':accessKey', ':usersPhone', ':reqPhone',':from', ':to') /;  

            $.ajax(  
                type:"GET",  
                url =  action(consumer: 'marcus', accessKey: key, usersPhone: myPhone, reqPhone: reqphone, from: fro, to: too),  
                success: function (response)  
                
                    $("#XMLArea").val(response);  
                
            );

    )
    </script>  

    <input type="button" id="request" class="button" name="request" style="float: left; top: 50px; left: 310px; position: absolute;"></input>  

我将添加更多信息,因为我已经更改了您提到的内容,但它仍然无法正常工作这里是反向测试控制器方法

public static void reversetest(@Required String consumer, @Required String accessKey, @Required String usersPhone,
                @Required String reqPhone, @Required Integer from, @Required Integer to)
    
        // Start time
            long _started = System.currentTimeMillis();

            // Send the query
            ResultObject result = models.functions.Reverse.v1(consumer, Telephones.ResponseTypes.CALLERID, accessKey, reqPhone,
                    usersPhone, from, to, (String) request.remoteAddress);
            // Statistics
            QueryCount++;
            if (result.Code != ResultCodes.OK)
                ErrorCount++;
            Long elapsed = (System.currentTimeMillis() - _started);
            QueryTime += elapsed;
            if (elapsed > MaxTime)
                MaxTime = elapsed;

            // Render
            if(result.Code == ResultCodes.NOTAUTHORIZED)
                render(result.Code);
            else if(result.Code == ResultCodes.TRANSFORMERROR)
                render(result.Code);
            else if(result.Code == ResultCodes.BADREQUEST)
                render(result.Code);
            else if(result.Code == ResultCodes.JAVAEXCEPTION)
                render(result.Code);
            else if(result.Code == ResultCodes.NOTHREADS)
                render(result.Code);


            renderXml(result.getXMLResponse());
    

这是完整的html代码

<head>
    <title></title>
    <style type="text/css">
        .button 
            -moz-box-shadow: inset 0px 1px 0px 0px #ffffff;
            -webkit-box-shadow: inset 0px 1px 0px 0px #ffffff;
            box-shadow: inset 0px 1px 0px 0px #ffffff;
            background: -webkit-gradient( linear, left top, left bottom, color-stop(0.05, #ededed), color-stop(1, #dfdfdf) );
            background: -moz-linear-gradient( center top, #ededed 5%, #dfdfdf 100% );
            filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ededed', endColorstr='#dfdfdf');
            background-color: #ededed;
            -moz-border-radius: 6px;
            -webkit-border-radius: 6px;
            border-radius: 6px;
            border: 1px solid #dcdcdc;
            display: inline-block;
            color: #777777;
            font-family: arial;
            font-size: 15px;
            font-weight: bold;
            padding: 6px 24px;
            text-decoration: none;
            text-shadow: 1px 1px 0px #ffffff;
        

            .button:hover 
                background: -webkit-gradient( linear, left top, left bottom, color-stop(0.05, #dfdfdf), color-stop(1, #ededed) );
                background: -moz-linear-gradient( center top, #dfdfdf 5%, #ededed 100% );
                filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#dfdfdf', endColorstr='#ededed');
                background-color: #dfdfdf;
            

            .button:active 
                position: relative;
                top: 1px;
            

        .text 
            border: 2px solid gray;
        

        .textarea 
            width: 900px;
            height: 300px;
            border: 3px solid gray;
            padding: 5px;
            font-family: Tahoma, sans-serif;
            background-image: url(bg.gif);
            background-position: bottom right;
            background-repeat: no-repeat;
        

        .label 
            color: #B4886B;
            font-weight: bold;
            display: block;
            width: 150px;
            float: left;
        

            label:after 
                content: ": ";
            
    </style>

    <script type="text/javascript">
    $("#request").click(function()

        var reqphone = $('#reqPhone').val();
        var key = $('#accesscode').val();
        var fro = $('#from').val();
        var too = $('#to').val();
        var myPhone = $('#userPhone').val();
        var action = #jsAction @reversetest(':consumer',':accessKey', ':usersPhone', ':reqPhone',':from', ':to') /;

    $.ajax(
        type:"GET",
        url:@Testbench.reversetest,
        data: = actionconsumer: 'marcus', accessKey: key, usersPhone: myPhone, reqPhone: reqphone, from: fro, to: too,
        success: function (response)
        
            $("#XMLArea").val(response);
        
    );

)
    </script>
    <script type="text/javascript">
        function setbg(color) 
            document.getElementById("styled").style.background = color
        


        //@Required String consumer, @Required String accesscode, @Required String usersPhone,
        //@Required String reqPhone, @Required Integer from, @Required Integer to


    </script>
</head>
<body style="background-color:#C3C3C3">
    <div style="float:left; position:absolute; left: 200px;">
        <header id="header1" style="width: 915px; height: 100px; border-bottom: 1px inset gray; background-image:url(pictures/mobisafeheader.jpg);" ></header>
        <br>
        <div id="sendDiv" style="float:left; top:120px; position:absolute;">
            <datalist id="numbers">
                <option>07086000000</option>
                <option>07074000000</option>
            </datalist>
            <datalist id="accesscodes">
                <option>marcus</option>
                <option>jesper</option>
            </datalist>

               <label class="label" id="phone">Your phone</label> 
               <input type="text" id="userPhone" class="text" list="numbers" style="float:left; left:150px; position:absolute;"></input>
                <br></br>
                <label class="label" id="accesscodeLbl" style="top:35px; float:left; position:absolute;">Your accesscode</label><input type="text" id="accesscode" class="text" list="accesscodes" style="float:left; left:150px; top:30px; position:absolute;"></input>
                <br></br>
                <label class="label" id="reqPhoneLbl" style="top:70px; float:left; position:absolute;">Calling Phone</label><input type="text" id="reqPhone" class="text" style="float:left; left: 150px; top: 60px; position: absolute;"></input>
                <input type="hidden" value="1" class="text" name="from"></input>
                <br></br>
                <input type="hidden" value="1" class="text" name="to"></input> 
                <input type="button" id="request" name="request" style="float: left; top: 50px; left: 310px; position: absolute;"></input>

        </div>
        <div id="textAreaDiv" style="position:absolute; float:left; top:250px;" draggable="false">
            <br></br>
            <br></br>
            <label class="label">URL</label>
            <input id="URL" type="text" class="text" style="width:745px;"></input>
            <br></br>
            <textarea id="XMLArea" class="textarea" draggable="false" onfocus="this.value=''; setbg('#e5fff3');" onblur="setbg('white')"></textarea>
        </div>
    </div>


</body>

希望这将帮助您认识到我的错误。

最好的问候。

【问题讨论】:

你正在使用url标签发送数据......它应该是发送数据的数据......并且应该使用url来点击 data:consumer: 'marcus', accessKey: key, usersPhone: myPhone, reqPhone: reqphone, from: fro, to: too, 没有action= 删除数据中的“动作”:actionconsumer: 'marcus'... 感谢大家的回答,调用 Jsaction 方法时,play 框架会生成一个可用的 url。我不得不将我的 【参考方案1】:

Url = 应该是data

$.ajax(  
    type:"GET",  
    url: 'url.url',
    data : action(consumer: 'marcus', accessKey: key, usersPhone: myPhone, reqPhone: reqphone, from: fro, to: too),  
    success: function (response)  
    
        $("#XMLArea").val(response);  
    
);

【讨论】:

您好,感谢您的回复,我在主帖中添加了一些额外的代码,如果您有时间可以查看它,也许会发现哪里出了问题!我添加了您建议的更改,但仍然无法正常工作。【参考方案2】:

你在 var key = $('#accesscode').val(); 处错过了一个 '

var key = $('#accesscode).val();

应该是

var key = $('#accesscode').val();

还有一件事

$.ajax(  
    type:"GET",  
    url =  action(consumer: 'marcus', accessKey: key, usersPhone: myPhone, reqPhone: reqphone, from: fro, to: too),  
    success: function (response)  
    
        $("#XMLArea").val(response);  
    
);

应该是

$.ajax(  
    type:"GET",  
    url : 'your url', 
    data: consumer: 'marcus', accessKey: key, usersPhone: myPhone, reqPhone: reqphone, from: fro, to: too,  
    success: function (response)  
    
        $("#XMLArea").val(response);  
    
);

【讨论】:

您好,我按照您的建议进行了更改,但仍然无法正常工作,无论如何我提交了一些额外的代码,以便您帮助我识别我的问题,感谢您的帮助!【参考方案3】:

缺少 URL 部分。

$.ajax(  
    type:"GET",  
    url: 'your url',
    data : action(consumer: 'marcus', accessKey: key, usersPhone: myPhone, reqPhone: reqphone, from: fro, to: too),  
    success: function (response)  
    
             // catch data coming back from server
    
);

使用数据部分向服务器发送数据。

【讨论】:

以上是关于ajax 请求需要协助的主要内容,如果未能解决你的问题,请参考以下文章

封装的ajax请求

使用 AJAX 请求时是不是还需要 Form 元素?

我是不是需要在 ajax 请求和接收该请求的 php 之间编码/解码查询参数?

找出完成一个 Ajax 请求需要多长时间

AJAX异步刷新 需要填写的参数

ajax 异步刷新,需要填写的参数