2012-06-18 51 views
8

我有输入域的形式,它可以像
如何将简单表单​​提交转换为ajax调用;

var algorithm = document.forms["algoForm"]["algorithm"].value; 
var input = document.forms["algoForm"]["input"].value; 

访问和先前调用未

document.forms["algoForm"].submit(); 

和形式是

<form name="algoForm" method="post" action="run.do"> 

这一切都运行正常
现在我想把它转换成ajax调用,所以th我可以使用从同一页面上的Java代码返回的数据。所以我用了类似

 var algorithm = document.forms["algoForm"]["algorithm"].value; 
     var input = document.forms["algoForm"]["input"].value; 
     var data = 'algorithm = ' + algorithm + '&input = ' + input; 


    $.ajax(
      { 
       url: "run.do", 
       type: "POST", 
       data: data, 
       success: onSuccess(tableData) 
     //line 75  { 
        alert(tableData); 
       } 

      } 
     ); 

但是上面的代码没有运行。请帮我使其运行

+0

首先都使用jQuery的序列化的http:// API。 jquery.com/serialize/将您的表单数据转换为“标准URL编码表示法中的文本字符串” –

+0

您是否收到一些js erros? –

+0

你能发布JavaScript错误或控制台日志吗? –

回答

1

我不知道如何,但这个运行良好,

var algorithm = document.forms["algoForm"]["algorithm"].value; 
    var input = document.forms["algoForm"]["input"].value; 



     $.post('run.do', { 
     algorithm : algorithm, 
     input : input 
     }, function(data) {     
     alert(data) 
    }); 
+0

由我的老师建议 – veer7

5

data需要一个文本对象,所以你需要:

var data = { 
    'algorithm': algorithm, 
    'input': input 
}; 
+0

当前版本的jQuery(['ajax()']的文档(http://api.jquery.com/jQuery.ajax/),截至15年6月)允许'数据参数是一个字符串,如果不是,它实际上会转换为一个URL安全的字符串。 – sameers

3

不是检索所有的参数值,然后分别向他们发送的(这是可以做到的服务器端,以及使用下面的代码),使用此:

var $form = $("#divId").closest('form'); 
    data = $form.serializeArray(); 

    jqxhr = $.post("SERVLET_URL', data) 
     .success(function() { 
      if(jqxhr.responseText != ""){ 
       //on response 
      } 
     }); 
    } 

divId是含有这种形式的div ID。

此代码将所有的表单参数发送到您的servlet。现在,您可以在servlet中使用request.getParameter来获取servlet上的所有单个字段值。

你可以轻松地将上面的jquery post转换成jquery ajax。

希望这有助于:)

9

试图使您的代码功能。试试这个:

var data = $("form[name=algoForm]").serialize(); 
$.ajax({ 
    url: "run.do", 
    type: "POST", 
    data: data, 
    success: function(tableData){ 
     alert(tableData); 
    } 
}); 
+0

是否var data = $(“form [name = algoForm]”)。serialize(); ?我是否需要包含任何文件? – veer7

+0

您需要的唯一东西就是jQuery库。 –

+0

是的,我敢肯定,如果你的表单名称是“algoForm”,就像上面发布的那样,该行应该可以正常工作。 –

0

// patching FORM - the style of data handling on server can remain untouched 
 
$("#my-form").on("submit", function(evt) { 
 
\t var data = {}; 
 
\t var $form = $(evt.target); 
 
\t var arr = $form.serializeArray(); // an array of all form items 
 
\t for (var i=0; i<arr.length; i++) { // transforming the array to object 
 
\t \t data[arr[i].name] = arr[i].value; 
 
\t } 
 
\t data.return_type = "json"; // optional identifier - you can handle it on server and respond with JSON instead of HTML output 
 
\t $.ajax({ 
 
\t \t url: $form.attr('action') || document.URL, // server script from form action attribute or document URL (if action is empty or not specified) 
 
\t \t type: $form.attr('method') || 'get', // method by form method or GET if not specified 
 
\t \t dataType: 'json', // we expect JSON in response 
 
\t \t data: data // object with all form items 
 
\t }).done(function(respond) { 
 
\t \t console.log("data handled on server - response:", respond); 
 
\t \t // your code (after saving) 
 

 
\t }).fail(function(){ 
 
\t \t alert("Server connection failed!"); 
 
\t }); 
 
\t 
 
\t return false; // suppress default submit action 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>