使用jquery ajax进行解析器错误?

时间:2011-10-10 17:07:03

标签: jquery ajax

这是我的代码,它返回一个解析器错误并失败。

$.ajaxSetup({
  type: 'GET',
  url: 'http://www.example.com/ajax',
  dataType: 'json'
});

$('#addHotlink').click(function() {
  $('body').after('<div id="lightBox" style="height: ' + htmlHeight + 'px;"><div><h3>Choose a name for your hotlink:<img id="lightBoxClose" src="http://www.example.com/images/closedAccordion.png" height="17" width="17" alt="" title="Close this box" /></h3><form id="addHotlinkForm" action="#" method="post" autocomplete="off"><input id="hotlinkName" type="text" maxlength="15" /><input class="submit" type="submit" value="Add Hotlink" /></form></div></div>');
  $('#lightBox').hide().fadeIn('fast');
  return false;
});

$('#addHotlinkForm').live('submit', function(event) {
  event.preventDefault();

  var pageURL = window.location.href;  
  var hotlinkName = $('#hotlinkName').val();

  if (hotlinkName.length > 0) {
    $.ajax({
      data: {
        hotlinkName: hotlinkName, 
        hotlinkURL: pageURL
      },
      success: function() {

        alert('Your hotlink ' + hotlinkName + ' has been inserted to the url: ' + pageURL);
      },
      error: function (ajaxOptions, thrownError) {

        alert(thrownError);
        alert('Var hotlinkName: ' + hotlinkName + ' Var hotlinkURL: ' + pageURL);
      }
    });
  }
});

尝试执行时出现解析器错误?虽然在错误函数中,值将返回为:

hotlinkName:文本输入框,

hotlinkURL:页面的当前地址(http://www.example.com/home)

为什么这不起作用?我尝试将类型更改为POST但它仍然无效?

这是我想要做的事情的一个小提琴,但当然它失败了,因为它没有链接到任何ajax表。

http://jsfiddle.net/novactown/QsDVe/

1 个答案:

答案 0 :(得分:1)

对我来说,如果你使用jsfiddle测试你的脚本将ajaxsetup更改为以下

$.ajaxSetup({

    type: 'GET',
        url: '/echo/json/',
    dataType: 'json'
});

我创建了this:一个简单的环境,可以在jsfiddle上使用JQuery测试基于jax的javascript。您可以查看jsfiddle documentation了解更多信息。