首先,我正在使用 ionic 框架开发移动应用程序
在一个工厂中,我需要与不在我的域中的服务器端通信(跨域请求)
我使用了这段代码,但它对我不起作用
$http.get({
url: 'http://test.wilson34.com/test.php',
data: {
"name": "peter"
}
}).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
console.log('Success' + data);
alert(data)
}).
error(function(data, status, headers, config) {
console.log('Status : ' + status);
});
但是当使用 jQuery 时,它会像魅力一样工作
jQuery.ajax({
url: 'http://test.wilson34.com/test.php',
data: {
"name": "peter"
}
}).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
console.log('Success' + data);
alert(data)
}).
error(function(data, status, headers, config) {
console.log('Status : ' + status);
});
请帮忙,我不会在我的项目中使用 jQuery,有什么建议吗? 它在 angularJs 中使用 $http
提前致谢
最佳答案
$http.get('http://test.wilson34.com/test.php', {params: {name: 'peter'}
}).success(function(data, status, headers, config) {
console.log('Success' + data);
}).error(function(data, status, headers, config) {
console.log('Status : ' + status);
});
Params 是一个具有键、值对的对象。在本例中,它的计算结果为 ?name=peter
关于javascript - AngularJs $http 不适用于跨域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27637974/