admin 管理员组文章数量: 1086019
I have my JavaScript files on my main domain and I want to do some calls from the subdomain.
I have added:
url: ".php"
So the full code is:
$.ajax({
type: "POST",
url: ".php",
data: {
var1: var1,
var2: var2
},
success: function(data){
}
});
But on Firebug it shows the request as red and it fails to respond. Also the POST parameters are there as they should.
Should I create a new JS file on the subdomain and add the necessary codes and do from there the AJAX calls?
EDIT: using JSONP code
I am using this on localhost/ajax.php
, which I call from sub.localhost
$.ajax({
dataType: 'jsonp',
data: 'id=10',
jsonp: 'jsonp_callback',
url: 'http://localhost/ajax.php',
success: function (data) {
console.log(data);
},
});
and the ajax.php contains:
<?php
echo $_GET["id"];
?>
I have my JavaScript files on my main domain and I want to do some calls from the subdomain.
I have added:
url: "http://domain./ajax.php"
So the full code is:
$.ajax({
type: "POST",
url: "http://domain./ajax.php",
data: {
var1: var1,
var2: var2
},
success: function(data){
}
});
But on Firebug it shows the request as red and it fails to respond. Also the POST parameters are there as they should.
Should I create a new JS file on the subdomain and add the necessary codes and do from there the AJAX calls?
EDIT: using JSONP code
I am using this on localhost/ajax.php
, which I call from sub.localhost
$.ajax({
dataType: 'jsonp',
data: 'id=10',
jsonp: 'jsonp_callback',
url: 'http://localhost/ajax.php',
success: function (data) {
console.log(data);
},
});
and the ajax.php contains:
<?php
echo $_GET["id"];
?>
Share
Improve this question
edited Oct 28, 2013 at 12:54
informatik01
16.5k11 gold badges79 silver badges108 bronze badges
asked Jun 11, 2011 at 15:52
stergoszstergosz
5,87013 gold badges64 silver badges134 bronze badges
3
- what are the 2 domains? is your server erroring out? – hvgotcodes Commented Jun 11, 2011 at 15:54
- at the moment i am testing at localhost, no errors, just doesnt return anything back. – stergosz Commented Jun 11, 2011 at 15:55
- try testing on the actual domains. localhost to domain is more problematic, though see possible solutions below. – glortho Commented Jun 11, 2011 at 16:04
3 Answers
Reset to default 4You can use Access-Control-Allow-Origin
header to enable cross-domain requests.
Read this: Cross-Origin Resource Sharing (CORS)
Assuming you have jQuery 1.5+ you can use:
$.ajax({
crossDomain:true,
type: "POST",
url: "http://domain./ajax.php",
data: {
var1: var1,
var2: var2
},
success: function(data){
}
});
From the DOCS:
crossDomain(added 1.5)
Default: false for same-domain requests, true for cross-domain requests
If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain
For subdomain calls you have two options:
Use document.domain on both sides.
Use jsonP, either via jQuery 1.5's crossDomain ajax specification, or directly.
本文标签: javascriptAJAX cross domain requestStack Overflow
版权声明:本文标题:javascript - AJAX cross domain request - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744097235a2533124.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论