Angular Js $ http.get CORS问题 - 在Jquery中工作
16lz
2021-01-22
Hi Guys I am new to angularjs, I am just converting my app from jquery to angular js. But I am facing difficulties with ajax request in angular js. My this code is working fine in jquery.
嗨大家我是angularjs的新手,我只是将我的应用程序从jquery转换为angular js。但我在角度js中遇到ajax请求的困难。我的这段代码在jquery中工作得很好。
Angularjs Code:
links.serverurl = My Another Server Address which is defined another location
$scope.submit = function () {
console.log(this.formData);
$http.get(links.serverUrl).success(function (response) {
console.log(response);
});
JQuery Working Code:
JQuery工作代码:
var url = "http://pbc.mydev786.com/";
$.get(url+"?"+data,function(response){
$("div[data-role='page']").hide();
$("#showbill").show();
$(".billresult").html(response);
});
My Php Headers:
我的Php标题:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PATCH, DELETE');
header('Access-Control-Allow-Headers: Origin, Content-Type,X-Requested-With');
AngularJs Config :
AngularJs配置:
$myapp.config(['$routeProvider', '$httpProvider', function ($routeProvider, $httpProvider) {
$routeProvider
.when('/', {templateUrl: "home.html"})
.when('/electricity', {templateUrl: "electricity.html"})
.when('/evo', {templateUrl: "evo.html"})
.when('/ptcl', {templateUrl: "ptcl.html"})
.when('/suigas', {templateUrl: "suigas.html"})
.when('/iesco', {templateUrl: "iesco.html"})
.when('/lesco', {templateUrl: "lesco.html"})
.when('/fesco', {templateUrl: "fesco.html"})
.when('/kesc', {templateUrl: "kesc.html"})
.when('/extra', {templateUrl: "extra.html"})
.when('/myaccount', {templateUrl: "myaccount.html"});
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);
Requested Headers :
请求的标题:
Accept application/json, text/plain, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length 20
Content-Type text/plain; charset=UTF-8
Host pbc.mydev786.com
Origin http://localhost:8383
Referer http://localhost:8383/PakistanCheckBill/
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0
Response Headers :
响应标题:
Connection keep-alive
Content-Encoding gzip
Content-Type text/html
Date Tue, 20 Jan 2015 11:47:43 GMT
Server nginx/1.6.2
Transfer-Encoding chunked
1 个解决方案
#1
2
Only this worked with me not any angularjs trick helped me out
只有这对我有用,没有任何angularjs技巧帮助我
# CORS Headers
<ifModule mod_headers.c>
Header always set Access-Control-Allow-Origin: "*"
Header always set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
Header always set Access-Control-Allow-Headers "X-Requested-With, content-type"
</ifModule>
i also tried to set these headers with php but that was not working i don't know why.
我也尝试用PHP设置这些标题,但这不起作用我不知道为什么。
更多相关文章
- PHP / Ajax:如何在成功登录后启动会话(剩余代码工作)
- php二维码/26行代码制作php二维码phpqrcode加grafika水印
- php分页代码的问题,显示了两个当前页码,求大神解答
- PHP开发微信支付代码及支付通知处理
- 几个有用的php字符串过滤,转换函数代码
- 在Web Page中包含PHP代码
- php mail函数一段好的代码
- 维基百科,如在PHP中列出标题的索引
- 用于上传多个文件的PHP代码