2014-05-21 2 views
1

jQuery .ajax 방법 fiddle is here을 통해 JSON 양식을 제출하는 데 2 ​​가지 문제가 있습니다. 현재 함수를 사용하여 양식 데이터를 가져 오면 'Uncaught TypeError : 순환 구조를 JSON으로 변환'오류가 발생하며 양식의 날짜 선택 도구에서 날짜를 제출해야한다고 생각합니다. 내가 바로 하나의 형식을 얻이 수없는 것양식을 JSON으로 변환하는 중 오류가 발생했습니다.

From json.org : "A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested."

, 나는 다음과 같은 데이터를 제출해야합니다

{ 
    "type": "value", 
    "identification": { 
     "ssn": "value" 
    }, 
    "date_of_birth": "value", 
    "name": { 
     "first": "value", 
     "middle": "value", 
     "last": "value" 
    }, 
    "address": { 
     "street1": "value", 
     "street2": "value", 
     "city": "value", 
     "state": "value", 
     "postal_code": "value", 
     "country_code": "value" 
    } 
} 

과 중첩 된 부모, 자녀 및 손자 중첩을 얻을하는 방법을 잘. 여기

function ConvertFormToJSON(form) { 
    var array = $(form).serializeArray(); 
    var json = {}; 

    $.each(array, function(){ 
    json[this.name] = this.value || ''; 
}); 
    return json; 
} 

및 제출

$('#USForm').validate({ 
    errorLabelContainer: "ul#msgBox", 
    wrapper: "li", 


    submitHandler: function(form) { 

     var form = this; 
     var json = ConvertFormToJSON(form); 
     $.ajax({ 
      type:'POST', 
      url:'http://ec2-54-201-216-39.us-west-2.compute.amazonaws.com/testc/WebService.asmx?op=VerifyIdentityJson', 
      crossDomain:true, 
      data: json, 
      dataType : 'json' 
     }) 
     .always(function(){ 
      console.log("always:" + json); 
     }) 
     .done(function (response){ 
      console.log("done:" + json); 
     }) 
     .fail(function(){ 
     console.log("fail" + json); 
     }); 
     return false; 
    }, 
    rules: { 
     //rules 
    }, 
    messages: { 
     //messages 
}); 
+0

오류가 발생하는 이유는 없습니다. –

+0

'jQuery.validation' 라이브러리를로드하지 않은 상태에서 문제를 재현하는 데모를 생성하십시오. – undefined

+0

죄송합니다. 바이올린 링크를 업데이트했습니다. –

답변

1

첫 번째 매개 변수 (형태이) 나를 위해 validator 인스턴스를 반환하려고 시도한은 다음과 같습니다 JSON의 funciton 내 변환입니다. currentForm 속성은 현재 form 요소를 제공합니다. 나는 양식 컨트롤을 그룹화하는 식별자를 사용하는 것이 좋습니다 것 JSON 구조를 만들기위한

, 내가 사용하고 data-group 다음 데모 속성 :

:

<input type="hidden" data-group="address" name="country_code" id="country_code" value="US"> 

이 그럼 당신은 루프를 사용하여 개체를 만들 수 있습니다

// Please do not use a StudlyCase name for a non-Constructor function 
function ConvertFormToJSON(form) 
{ 
    var elems = [].slice.call(form.elements); 
    var o = {}; 
    $.each(elems, function() { 
     var $this = $(this), 
      g = $this.data('group'); 

     // if the current element has data-group attribute 
     if (g) { 
      // if the object doesn't have `group` property yet 
      // create an object so we can set a property to it later 
      if (!o.hasOwnProperty(g)) { 
       o[g] = {}; 
      } 
      o[g][this.name] = this.value; 
     } else { 
      // otherwise set a top level property 
      o[this.name] = this.value; 
     } 
    }); 

    return o; 
} 

유효성 확인을 위해 단지 rules 속성을 조작했음을 유의하십시오.

http://jsfiddle.net/4nLLM/

+0

정말 멋진 사람입니다. 많은 질문이 있습니다. 정말 대단 하네. 내가 할 수 있다면 맥주를 사줄거야. –

관련 문제