Ajax:在Cross子域上调用api

时间:2015-07-18 16:50:19

标签: ajax .htaccess

我正在尝试使用此代码对我的crm api进行ajax调用,但无法正常工作:

var username = 'admin';
var password = '1';

$.ajax
({
  type: "GET",
  dataType: 'json',
  async: false,
  url: "http://demo.espocrm.com/basic/api/v1/App/user",
  headers: {
    "Authorization": "Basic " + btoa(username + ":" + password)
  }
})
.done(function(){
    alert('Authenticated!')
})
.fail(function(){
    alert('Error!')
});

来电从www.domain.comcrm.domain.com

.htaccess crm.domain.com上的

我有以下代码:

<ifModule mod_headers.c>
   Header set Access-Control-Allow-Origin "http://www.domain.com"
   Header set Access-Control-Allow-Methods "OPTIONS, GET, PUT, POST, DELETE, HEAD"
   Header set Access-Control-Max-Age "1800"
</ifModule>
  • 我还尝试使用jsnot并使用此网址:http://demo.espocrm.com/basic/api/v1/App/user?callback=?

错误是:

OPTIONS http://crm.domain.com/api/v1/App/user?_=1437237158381 500 (Internal Server Error)


XMLHttpRequest cannot load http://crm.domain.com/api/v1/App/user?_=1437237158381. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.domain.com' is therefore not allowed access. The response had HTTP status code 500.

当我使用jsnop时:

GET http://crm.domain.com/api/v1/App/user?callback=jQuery21404605716757941991_1437238651212&_=1437238651213 

0 个答案:

没有答案