对外部api的Ajax请求调用返回了[Object object]

时间:2019-09-11 12:41:45

标签: ajax api

我想显示来自API端点的内容。这是公司在其网站上的地址

但是我得到了[object object]

我已经尝试过几次更改代码,但是运气不错

 $(document).ready(function() {
                $.ajax({
                    type: 'GET', // define the type of HTTP verb we want to use (POST for our form)
                    url: 'http://datarecapture.premiumpension.com:8089/api/ContactUs/Contact', 
                    data: JSON.stringify(), // our data object
                    contentType: "application/json; charset=utf-8", // what type of data do we expect back from the server
                    dataType: "json",
                     success: function (result)  {
                    // console log data to the console so we can see
                     alert("Success " + result);
                  },
                // here we will handle errors and validation messages
                  error: function (ErrorMessage) 
                  {  //debugger
                      alert("Error Message " + ErrorMessage);
                    }
                })
            })

1 个答案:

答案 0 :(得分:0)

您可以尝试

func loadImageWithURL(url: URL, callback: @escaping (UIImage, URL) -> ()) {
print("This is getting excuted loadImageWithURL")
let session = URLSession.shared
let downloadTask = session.downloadTask(with: url, completionHandler: {
     url, response, error in

    if error == nil && url != nil {
        if let data = NSData(contentsOf: url!) {
            if let image = UIImage(data: data as Data) {
                DispatchQueue.main.async(execute: {
                    callback(image, url!)
                })
            }
        }
    }
})
     downloadTask.resume()
}

最有可能在出现数据警报时

let stationCollectionViewcell : StationCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "stationCell", for: indexPath) as! StationCollectionViewCell

if imageURL.contains("http") {
   let url = URL(string: station.imageURL)!
   self.loadImageWithURL(url: url) { (image, callbackUrl) in
       guard url == callbackUrl else { return }
       stationCollectionViewcell.radioImgView.image = image
   }
} else if imageURL != "" {
   stationCollectionViewcell.radioImgView.image = UIImage(named: "station-therockfm")
} else {
   stationCollectionViewcell.radioImgView.image = UIImage(named: "stationImage")
}

将起作用。

请先检查数据如何进入控制台