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
Add a ment  | 

3 Answers 3

Reset to default 4

You 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:

  1. Use document.domain on both sides.

  2. Use jsonP, either via jQuery 1.5's crossDomain ajax specification, or directly.

本文标签: javascriptAJAX cross domain requestStack Overflow