简体   繁体   English

无法通过ajax将String传递给控制器

[英]Can't pass String to controller through ajax

I'm new of web development with play framework and ajax,Now I want to pass the string in the form to controller with ajax but I don't know how to do.Could you please help me?Here is my code: html: 我是使用play框架和ajax进行web开发的新手,现在我想将表单中的字符串传递给带有ajax的控制器,但我不知道该怎么做。请你帮我吗?这是我的代码:html:

<form onsubmit="return newSearch();" id="formId">
    <input type="search" placeholder="Search for more" id="searchBar_chat">
</form>
<script type="text/javascript" >
function newSearch()
{
    var s = document.getElementById("chatDialgue");
    var searchValue = document.getElementById("searchBar_chat").value;
    s.innerHTML = s.innerHTML + '<li>'+ searchValue +'</li>';
    document.getElementById("searchBar_chat").value ="";
    $.ajax({
        url: "DataMatch/searchContentMatch",  
        type:"GET",
        cache: false,
        dataType:"text",
        data:"searchValue",
        success: function (responseData) {  
        s.innerHTML = s.innerHTML + '<li>'+responseData+'</li>';  
        }  
        });
    return false;
}
</script>

controller: 控制器:

public class DataMatch extends Controller{

  public String searchContentMatch (String search) {
    // Search match
    return "HI"+search;
  }

}

Pass it as a Query Parameter since it is just a single string. 将其作为查询参数传递,因为它只是一个字符串。

var search="searchValue";
 $.ajax({
        url: "DataMatch/searchContentMatch?search="+search,  
        type:"GET",
        cache: false,
        dataType:"text",
        success: function (responseData) {  
        s.innerHTML = s.innerHTML + '<li>'+responseData+'</li>';  
        }  
        });

First of all, in the data parameter of $.ajax() you have to build a query string, so the correct way is concatenating the key ("search") with its value (searchValue). 首先,在$.ajax()的data参数中,您必须构建一个查询字符串,因此正确的方法是将密钥(“search”)与其值(searchValue)连接起来。

Then, you don't need neither that return false statement in the script nor the return keyword on the onsubmit form attribute. 然后,您既不需要在脚本中return false语句,也不需要在onsubmit表单属性上return关键字。

Lastly, since you're using jQuery, take advantage of its selection capabilities and replace document.getElementById with $ . 最后,由于您正在使用jQuery,请利用其选择功能并将document.getElementById替换为$

<form onsubmit="newSearch();" id="formId">
    <input type="search" placeholder="Search for more" id="searchBar_chat">
</form>
<script type="text/javascript" >
function newSearch(){
    var searchValue = $("#searchBar_chat").val();
    $("#chatDialgue").append('<li>'+ searchValue +'</li>');
    $("#searchBar_chat").val("");
    $.ajax({
        url: "DataMatch/searchContentMatch",  
        type:"GET",
        cache: false,
        dataType: "text",
        data: "search=" + searchValue,
        success: function (responseData) {  
            $("#chatDialgue").append('<li>' + responseData + '</li>');
        }  
    });
}
</script>

You should use the Javascript router instead of hardcoding the endpoint in your Javascript code. 您应该使用Javascript路由器,而不是在Javascript代码中硬编码端点。 This will allow you to change the endpoint name later on if needed. 这将允许您稍后在需要时更改端点名称。 Also, you want to pass a query string parameter, so it goes directly in the URL. 此外,您希望传递查询字符串参数,因此它直接在URL中传递。 Here is how to use the reverse route: 以下是如何使用反向路线:

@helper.javascriptRouter("jsRoutes")(
    routes.javascript.DataMatch.searchContentMatch
)

//HTML here

var endpoint = jsRoutes.controllers.DataMatch.searchContentMatch(searchValue).url;
$.ajax({
  url: endpoint,  
  type:"GET",
  cache: false,
  success: function (responseData) {  
  s.innerHTML = s.innerHTML + '<li>'+responseData+'</li>';  
  }  
});

还有一点,对于上一个答案,请不要忘记routes文件中的参数:

GET    /api/search/:search             controllers.DataMatch.searchContentMatch(search: String)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM