weixin_33747129 2017-04-24 04:51 采纳率: 0%
浏览 89

使用ajax获取数据

I am trying to get JSON data from an API by using this code:

$.ajax({

xhrFields: {
    withCredentials: true
},
beforeSend: function (xhr) {
    xhr.setRequestHeader('Authorization', 'Basic ' + btoa('user:pass'));
},

url: 'http://127.0.0.1/appcpanel/appapi/tribe',
type: 'GET',
dataType: 'jsonp',
success: function (data) {
console.log(data);
       },
});

In a Mobile app by cordova and framework7.

The browser console gives me the following error :

XMLHttpRequest cannot load http://127.0.0.1/appcpanel/appapi/tribe. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.2:3000' is therefore not allowed access. The response had HTTP status code 404.
  • 写回答

5条回答 默认 最新

  • weixin_33721344 2017-04-24 04:58
    关注

    Try this.

    var Variable = "1";
            $.ajax({
                type: "POST",
                url: "url here",
                data: '{"ID" : ' + Variable + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (jsonData) {
                    if (jsonData.d != "") {
                        if (jsonData.d != "")
                        {
                            console.log(jsonData.d)
                        }
                    }
                }

    </div>
    
    评论

报告相同问题?