javascript - AngularJs $http 不适用于跨域

标签 javascript jquery ajax angularjs

首先,我正在使用 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/

相关文章:

javascript - 如何使用数据属性和 jQuery 使链接可点击或不可点击?

jquery - 使用 JQuery 播放/暂停 HTML 5 视频

javascript - 当从 ajax 中单击“下一步”时,数据表会获取接下来的 10 个项目

javascript - React js 中的条件属性

javascript - 获取异步函数返回的数据

javascript - 为什么在 jQuery 单击事件触发我的 es6 类函数之一后,我的其他类函数和变量变得未定义

javascript - iPhone 网络中的 .focus() 问题

javascript - 触发单击单选按钮作为文档准备就绪的一部分

javascript - .data() JQuery 返回未定义

javascript - window.location.reload(true) 没有正确重新加载页面