通过Ajax传递数据获取json文件

时间:2012-10-18 18:01:11

标签: jquery ajax json

我正在开发一些代码,用户可以在字段中输入邮政编码,如果用户点击发送时通过客户端验证我希望脚本将邮政编码发送到地址和后端开发人员已设置它,以便它将返回一个有效或无效的json文件。

问题是我现在回来的都是,未定义。

有人可以看到我做错了什么或给出一些建议或解决问题。我不太确定如何使用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);
    }
 })

1 个答案:

答案 0 :(得分: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);
    },