2016-04-12 42 views
1

我有六个字段和一个搜索按钮的搜索表单。当用户填写表单deatails并单击搜索按钮时,我需要使用jQuery ajax将表单字段值作为json发送到服务器。如何获取表单字段值并将其作为json使用ajax发送到服务器jquery

然后服务器将发送搜索值并返回响应,然后我需要在ui中填充这些valuse。我需要为jquery ajax提供示例UI代码。请任何人都可以帮忙吗?下面 是我的HTML表单

<form name="NAME" id="customerDetailSearchForm" action=""> 
    <fieldset> 
    <legend>Search Contact</legend> 
    <table width="100%" cellpadding="0" cellspacing="0" class="vzuui-detailpanel"> 
     <tr> 
      <td><label>Name :</label><input type="text" value="" /></td> 
      <td><label>City :</label><input type="text" value="" /></td> 
      <td><label>Phone :</label><input type="text" value="" /></td> 
     </tr> 
     <tr> 
      <td><label>Address :</label><input type="text" value="" /></td> 
      <td><label>State Prov :</label><input type="text" value="" /></td> 
      <td><label>Email :</label><input type="text" value="" /></td> 
     </tr> 
    </table> 
    </fieldset> 
    <button class="vzuui-btn-red-active closeedit" type="button" id="search">Search</button> 

+0

'$( '#customerDetailSearchForm')。序列化()'会抓住所有的'data'从'form' – Rayon

+0

jQuery的。 com将 – pathfinder

+0

$('#customerDetailSearchForm')。serialize()存储到单个变量并发送到服务器 –

回答

0

所有你需要的,因为我们将使用jQuery serializeArray() Methodcreates an array of objects (name and value) by serializing form values.

现在改变你的HTML表单代码包含在文本框name属性像下面

<form name="NAME" id="customerDetailSearchForm" action=""> 
    <fieldset> 
    <legend>Search Contact</legend> 
    <table width="100%" cellpadding="0" cellspacing="0" class="vzuui-detailpanel"> 
     <tr> 
      <td><label>Name :</label><input type="text" value="" name="name" /></td> 
      <td><label>City :</label><input type="text" value="" name="city" /></td> 
      <td><label>Phone :</label><input type="text" value="" name="phone" /></td> 
     </tr> 
     <tr> 
      <td><label>Address :</label><input type="text" value="" name="address" /></td> 
      <td><label>State Prov :</label><input type="text" value="" name="state" /></td> 
      <td><label>Email :</label><input type="text" value="" name="email" /></td> 
     </tr> 
    </table> 
    </fieldset> 
    <button class="vzuui-btn-red-active closeedit" type="button" id="search">Search</button> 

这是必需的一jQuery部分从表单中形成JSON数据并进行AJAX调用。

<script type="text/javascript"> 
$(document).ready(function(){ 
    $("#search").click(function(){ 

    var frm = $("customerDetailSearchForm").serializeArray(); 
     var obj = {}; 
     for (var a = 0; a < frm.length; a++) { 
     obj[frm[a].name] = frm[a].value; 
     } 
     var jsonData = JSON.stringify(obj); 

    $.ajax({ 
     type: 'GET', 
     url: 'http://example.com', 
     data: jsonData , 
     dataType: 'json', 
     success: function (data) { 
      // add result to UI code over here 
     } 
    }); 
    }); 
}); 
</script> 

EDIT

上面JavaScript代码段编辑,以产生适当的JSON值

+0

嗨Aniket, 我已经实施了乌尔代码,但即时通讯错误。 当我使用var jsonData时,json值也不能正确显示jsonData = JSON.stringify(frm); 控制台错误: http://url.com?[{"name":"name","value":"jhon"},{"name":"city","value":"us“} { “名”: “手机”, “值”: “9897896581”},{ “名”: “地址”, “值”: “Colianielna”},{ “名”: “状态”, “价值”: “Ustance”},{“name”:“email”,“value”:“[email protected]”}] 405(方法不允许) –

+0

你好Ganesh,我共享的代码使用GET方法..你可以看到我提到它是类型:'GET'。 HTTP 405错误意味着你使用了错误的方法,所以我假定你正在调用的服务使用POST方法。确认相同并相应地使用适当的方法。至于不正确的JSON值检查我编辑的答案 –

+0

嗨Aniket,我做了这些变化,现在它工作正常。感谢您帮助我解决这个问题。 –

0

在这里你去

Convert form data to JavaScript object with jQuery

但是你必须添加name属性为每个输入元素之前。

因此该名称将是您的JSON键,然后在框中的数据将是关键的值,如下

<input name='username' value='' />

将成为

{"username": "john"}

JHON的输入值在输入框中。

**编辑(Ajax代码)**

$(function() { 
    $('#customerDetailSearchForm').submit(function() { 
    $.post("//your URL here",JSON.stringify($('#customerDetailSearchForm').serializeObject()), function(result){ 

     alert("Data posted successfully!!"); 

    }); 
    }); 
}); 

或更换以下行,如果你没有在提交表单按钮

$('#customerDetailSearchForm').submit(function() { 

$('#search').click(function() { 
+0

好的。我需要样本ajax代码击中服务。可以ü给我的样品阿贾克斯代码打到服务 –

+0

在帖子中修正,试试吧 – kakurala

+0

大家好。我使用了@Aniket代码。现在它工作正常。谢谢所有 –

0

这里简单的Ajax代码,我在Asp.net中使用MVC 我想这会帮助你,

$.ajax({ 
       beforeSend: function (xhr) { 
        AjaxRequestStart(); 
       }, 
       error: function (result) { 
            }, 
       complete: function() { 
        AjaxRequestFinish(); 
       }, 
       url: '@Url.Content("~/Project/SaveProject")', 
       type: 'POST', 
       data: $('#frmAddProject').serialize(), 
       success: function (result) { 

       } 
      }); 
0

在这里,我与@Aniket同意。为每种输入类型首先添加名称。

<form name="NAME" id="customerDetailSearchForm" action="your_url"> 
    <fieldset> 
    <legend>Search Contact</legend> 
    <table width="100%" cellpadding="0" cellspacing="0" class="vzuui-detailpanel"> 
     <tr> 
      <td><label>Name :</label><input type="text" value="" name="name" /></td> 
      <td><label>City :</label><input type="text" value="" name="city" /></td> 
      <td><label>Phone :</label><input type="text" value="" name="phone" /></td> 
     </tr> 
     <tr> 
      <td><label>Address :</label><input type="text" value="" name="address" /></td> 
      <td><label>State Prov :</label><input type="text" value="" name="state" /></td> 
      <td><label>Email :</label><input type="text" value="" name="email" /></td> 
     </tr> 
    </table> 
    </fieldset> 
    <button class="vzuui-btn-red-active closeedit" onclick="_submit();" type="button" id="search">Search</button> 

对于调用ajax,你可以使用它。

function _submit{ 
     $.ajax({ 
       type: 'POST', 
       dataType: 'json', 
       url: 'your_url', 
       data: $("#customerDetailSearchForm").serialize(), 
       success: function(responseData, textStatus) { 
        // you implementation logic here 
       }, 
       complete: function(textStatus) { 

       }, 
       error: function(responseData) 
       { 

       } 
      }); 
     } 
0

您可以参考下面的示例Ajax调用的形式提交

 

    $("#customerDetailSearchForm").submit(function(e) { 

      var postData = $('#conatctUs').serializeArray(); 
      var formURL = $('#conatctUs').attr("action"); 
      var formName = $('#conatctUs').attr('name'); 

      $.ajax({ 
        url : formURL, 
        type: "POST", 
        data : postData, 
        success:function(data) 
        { 
         if(data.status){ 
          console.log(data); 
         } 
        }, 
        error: function(jqXHR, textStatus, errorThrown) 
        { 

        } 


      e.preventDefault(); 
    }); 

相关问题