Javascript AngularJs$http不适用于跨域
首先,我正在使用ionic框架开发一个移动应用程序 在一个工厂中,我需要与不在我的域中的服务器端对话(跨域请求) 我使用了这个代码,但它从来都不适用于我Javascript AngularJs$http不适用于跨域,javascript,jquery,ajax,angularjs,Javascript,Jquery,Ajax,Angularjs,首先,我正在使用ionic框架开发一个移动应用程序 在一个工厂中,我需要与不在我的域中的服务器端对话(跨域请求) 我使用了这个代码,但它从来都不适用于我 $http.get({ url: 'http://test.wilson34.com/test.php', data: { "name": "peter" } }). success(function(data, status, headers, config) { // this callba
$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
服务的方式不对$http()
是服务,而不是需要从此服务调用get()
或post()
方法的函数:
$http.get('http://test.wilson34.com/test.php?name=peter').
success(function(data, status, headers, config) {
console.log(data);
}).
error(function(data, status, headers, config) {
console.log('Status : ' + status);
});
结帐
Params是一个具有键、值对的对象。在本例中,它的计算结果为?name=peter基本上您对
$http
的语法是错误的。像angular js中使用的服务一样使用它,不要忘记将$http用作承载
$http
请求的方法的参数
$http.get(URL)
.success(function(response) {
console.log('success');
})
.error(function(response){
console.log('error')
});
谢谢大家,我说我必须把数据放在一个param对象中解决它
最终代码为:
$http({
url : 'http://test.wilson34.com/test.php',
type : 'GET',
params : {
'name' : encrypted
}
}).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);
});
尝试在angular配置块中设置$httpProvider.defaults.useXDomain=true它将失败,因为您使用的是jQuery的风格,而不是angular的AJAX风格。是的,我确实知道,而且我使用的是$http.get,但我只是在问题中键入了错误谢谢您的建议,但它不起作用
$http({
url : 'http://test.wilson34.com/test.php',
type : 'GET',
params : {
'name' : encrypted
}
}).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);
});