2013-05-27 4 views
-1

Здравствуйте, я использую Play framework 1.2.3 с eclipse, я хочу вызвать метод контроллера с этим Java-запросом, но когда я нажимаю кнопку, ничего не происходит. Кто-нибудь есть идея, что пошло не так?Ассистент требуется с ajax-запросом

С наилучшими пожеланиями.

<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> 

я добавлю еще немного информации, потому что я уже изменил вещи, которые вы упомянули, но он все еще не работает в любом случае здесь является reversetest метод контроллера

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: = action{consumer: '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> 

Надеюсь, это поможет вам распознать мои ошибки.

С наилучшими пожеланиями.

+1

где ваш адрес? –

+1

вы отправляете данные с помощью URL-адреса url ... это должны быть данные для отправки данных .. и URL-адрес должен использоваться для удаления –

+0

'data: {потребитель: 'marcus', accessKey: key, usersPhone: myPhone, reqPhone: reqphone, from: fro, to: too}, 'without' action' или '=' – Spokey

ответ

0

Вы пропустили 'at 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); 
    } 
}); 
+0

Спасибо! Я только что исправил эту ошибку, но она все еще не работает. – Mac

+0

Здравствуйте, я внес изменения, которые вы предложили, но он все еще не работает, так или иначе я представил дополнительный код, чтобы вы могли помочь мне распознать мою проблему, спасибо за помощь! – Mac

0

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); 
    } 
}); 
+0

Здравствуйте, спасибо за ответ, я добавил дополнительный код на главный пост, если у вас есть время, вы можете посмотреть на него и, возможно, определить, что пошло не так! Я добавил изменения, которые вы предложили, и он все еще не работает. – Mac

0

Отсутствует 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 
    } 
}); 

Используйте данные для отправки данных на сервер.

Смежные вопросы