成功函数在$ .ajax中不起作用

时间:2015-03-02 08:38:04

标签: jquery ajax

我尝试通过$ .ajax从API中获取一些数据我设法在我的控制台中获得响应,因此调用没有任何问题,但是成功功能不起作用,我甚至试图放在成功功能中发出警报但仍然无法正常工作。

    var symptoms = sessionStorage.getItem('resultSymptoms') ;
    var sendData =  {"patientID":{"ICD9":symptoms,
    "text_type":"long",
    "PatientName":"SomeName",
    "DoctorsName":"SomeName"}}

         $.ajax({
    type: "POST",
    url:  "http://130.211.170.206:8080/freemium",
    contentType :"application/json",
    headers: {          
         Accept : "text/html"         
    }  ,
    dataType: "json",
    data: JSON.stringify(sendData),
      success: function(data) {
        console.log('This is the data '+data.text()) ;
          $('#resultTemplate').html(data.text()) ;
                },
      failure: function(data) {
         console.log(' failed to connect');
      }

});
});

编辑:
当我把错误回调函数我得到解析错误

编辑2:当我将数据类型更改为' text'

2 个答案:

答案 0 :(得分:1)

试试这个:

$.ajax({
    type: "POST",
    url:  "http://130.211.170.206:8080/freemium",
    contentType :"application/json",
    headers: {          
         Accept : "text/html"         
    }  ,
    dataType: "json",
    data: JSON.stringify(sendData)
})
      .done(function(data) {
          console.log('This is the data '+data.text()) ;
          $('#resultTemplate').html(data.text());
      })
      .error(function(data) {
         console.log(' failed to connect');
      });

答案 1 :(得分:0)

使用error属性指定错误处理程序,以便您查看错误消息:

var symptoms = sessionStorage.getItem('resultSymptoms');
var sendData = {
  "patientID": {
    "ICD9":symptoms,
    "text_type":"long",
    "PatientName":"SomeName",
    "DoctorsName":"SomeName"
  }
};

$.ajax({
  type: "POST",
  url: "http://130.211.170.206:8080/freemium",
  contentType :"application/json",
  headers: {          
    Accept : "text/html"         
  },
  dataType: "json",
  data: JSON.stringify(sendData),
  success: function(data) {
    console.log('This is the data ' + data.text());
    $('#resultTemplate').html(data.text());
  },
  error: function(xhr, stat, err) {
    console.log('Error: ' + stat + ' ' + err);
  }
});