2012-10-18 57 views
1

我正在處理一些代碼,該代碼會讓用戶在字段中輸入郵政編碼,如果在用戶單擊發送時通過客戶端驗證我希望腳本發送郵政編碼代碼到一個地址,後端開發者已經設置了它,以便它將返回一個有效或無效的json文件。通過Ajax傳遞數據獲取json文件

問題是,我現在回來的是undefined。

有人可以看到我做錯了什麼,或給一些建議或疑難解答。我真的不知道如何使用console.log,這可能會幫助我。謝謝。

jQuery.validator.addMethod("postalcode", function(postalcode, element) { 
return this.optional(element) || postalcode.match(/(^\d{5}(-\d{4})?$)| (^[ABCEGHJKLMNPRSTVXYabceghjklmnpstvxy]{1}\d{1}[A-Za-z]{1} ?\d{1}[A-Za-z]{1}\d{1})$/); 
}, "Zip code not valid. Please try again."); 

var myForm = $('#zipform'); 

myForm.validate({ 
    errorClass: "errormessage, intro-highlighted", 
    onkeyup: false, 
    highlight: function(element) { 
     $(element).addClass('intro-highlighted'); 
    }, 
    unhighlight: function(element) { 
     $(element).removeClass('intro-highlighted'); 
    }, 

    errorClass: 'error', 
    validClass: 'valid', 
    rules: { 
    zipcode: { required: true, postalcode: true } 
    }, 
    errorPlacement: function(error, element) 
    { 
    // Set positioning based on the elements position in the form 
    var elem = $(element), 
     corners = ['left center', 'right center'], 
     flipIt = elem.parents('span.right').length > 0; 

    // Check we have a valid error message 
    if(!error.is(':empty')) { 
     // Apply the tooltip only if it isn't valid 
     elem.filter(':not(.valid)').qtip({ 
     overwrite: false, 
     content: error, 
     position: { 
      my: 'top left', 
      at: 'top left', 
      target: $('#zipcode'), 
      adjust: { 
      y: -30 
      } 
     }, 

     show: { 
      event: false, 
      ready: true 
     }, 
     hide: false, 
     }) 

     // If we have a tooltip on this element already, just update its content 
     .qtip('option', 'content.text', error); 
    } 

    // If the error is empty, remove the qTip 
    else { elem.qtip('destroy'); } 
    }, 
    success: $.noop, // Odd workaround for errorPlacement not firing! 
}) 

$.ajax({ 
    url: "http://address/icantshow/json/checkZip.action?zipCode=" + zipcode + "?jsoncallback=?", 
    type: "GET", 
    async: false, 
    dataType: "jsonp", 
    /*data: { 
     zipcode:$('#zipcode').val(), 
     username: 'user', 
     password: 'pass' 
     },*/ 
    success: function(json) { 
     console.log(typeof data); 
     //console.log(jsondata); 
     //$('.bar').css({display:'none'}); 
     //$('.loader').append(data); 
    }, 
    error: function(e) { 
     console.log(e.message); 
    } 
}) 
+0

'異步:FALSE'在JSONP請求沒有任何意義,並通過jQuery的忽略。 –

回答

1
//REPLACE 
url: "http://address/icantshow/json/checkZip.action?zipCode=" + zipcode + "?jsoncallback=?", 
//BY 
    url: "http://address/icantshow/json/checkZip.action?zipCode=" + zipcode + "&jsoncallback=?", 

//IN 
success: function(json) { 
     console.log(typeof data); 
    }, 

//SHOULD BE 
success: function(json) { 
     console.log(json); 
    }, 
+0

爲什麼downvote?這個迴應中的兩個建議都表明需要發生的修復。 –

+0

感謝您的更正,但它仍然返回undefined。任何其他的想法。 – xoam