dongyi7966 2016-04-29 16:35
浏览 475
已采纳

jQuery Ajax不会返回数据(解析错误),但Postman成功

I've been using Postman to test my REST API (written in Go). Now is the moment of truth, connecting to the front end and I've hit a hiccup. I'm using jQuery.ajax to send a GET to my server, which should then send me back the response.

Note: Going to postman and sending a GET request to http://REMOTE_IP:8000/users/ returns the information I'm looking for, an array of User objects (JSON). It confuses me that it would work for Postman, but not jQuery.

jQuery Get

$.ajax({
    url: "http://REMOTE_IP:8000/users/",
    type: "GET",
    dataType: 'jsonp',
    contentType: 'application/json',
    success: function(data) {
        console.log(data);
    },
    error: function(jqXHR, status, err) {
        console.log(jqXHR);
        console.log(status);
        console.log(err);
    }
});

Go Backend

func GetUsersRoute(w http.ResponseWriter, r *http.Request) {
    var queryResult []models.User

    queryErr := Config.Users.Find(bson.M{}).All(&queryResult)
    resultsJSON, jsonErr := json.Marshal(queryResult)

    handleQueryAndJsonErrors(w, queryErr, jsonErr)

    // added these for cors, not sure they do anything helpful
    w.Header().Set("Content-Type", "application/json; charset=UTF-8")
    w.Header().Set("Access-Control-Request-Headers", "*")

    w.WriteHeader(http.StatusOK)
    w.Write(resultsJSON)
}

I also have a logger attached to my go backend, and here are the request method and request URI that are printed out when I make the request:

GET /users?callback=jQuery223019655483671137586_1461946990647&_=1461946990648

Here are some articles I found useful:
The data which jquery $.ajax function return could not be parse to json

https://github.com/codegangsta/martini-contrib/blob/master/cors/cors.go

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Access-Control-Allow-Methods

Here are the errors returned with jQuery's err callback: enter image description here

What could be going wrong?

  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 edge跟谷歌浏览器的默认搜索引擎无法修改,如何解决?
      • ¥15 access数据库出错
      • ¥100 matlab数学建模习题,求解答
      • ¥15 blender python输出纹理坐标(像素级)与三维坐标的对应关系
      • ¥15 python文件分发
      • ¥15 关于#c语言#的问题:要求:输出格式要界面直观、清晰大方、格式规范
      • ¥15 计算机组成汇编语言电路升级
      • ¥15 hadoop网站点击流数据分析项目
      • ¥15 TDA4 USB 识别不到的问题
      • ¥36 禁用ssl重协商以达到解决DOS漏洞