如何让php脚本从另一个域(跨域)生成html

时间:2022-08-23 10:24:30

i am wondering how can get the HTML code which is generated by a cross-domain php script?

我想知道如何获得由跨域php脚本生成的HTML代码?

Normally if i'm on the same domain , i would use Ajax as follows:

通常,如果我在同一个领域,我会使用Ajax如下:

$.ajax({
    type: 'GET',
    url: 'user.php',
    data: 'user_id=user_id', //assuming user_id value was already set.
    success: function(html)
    {
        $('#info').empty().html(html);
    }  
});

But i am now working on a different domain than my server domain. Which means i use JSON to send data back and to my server php scripts. However , i know that JSON only send data but not a complete HTML CODE(or am i missing out some point here?)

但我现在的工作领域与我的服务器领域不同。这意味着我使用JSON将数据发送回服务器php脚本。但是,我知道JSON只发送数据而不发送完整的HTML代码(或者我在这里漏掉了什么?)

So , how can i get the html code generated by a cross-domain php script(server) to my web page(another domain).

因此,如何将跨域php脚本(服务器)生成的html代码传递到我的web页面(另一个域)。

6 个解决方案

#1


2  

using javascript you can do the same as if it was JSON, it's called JSONP the P is with padding.

使用javascript,您可以像使用JSON那样做,它被称为JSONP, P带有填充。

Or you can call it JSON with callback:

或者你可以用回叫来调用JSON:

// Request Page

/ /请求页面

myCallback("Some string or Object to parse to your site");

// Your Page

/ /页面

window["myCallback"] = function(string_or_object) {
    // Here you can do everything with the parsed data
}

Create a script-tag and include the request page. Make sure to define your callback before including the script-tag

创建一个脚本标记并包含请求页面。在包含脚本标记之前,请确保定义回调

or you can use jQuery's ajax method with dataType set to jsonp:

或者您可以使用jQuery的ajax方法,将数据类型设置为jsonp:

$.ajax({
    "url": "requst_page.php",
    "dataType": "jsonp",
    "success": function(string_or_object) {
        // Here you can do everything with the parsed data
    }
})

Look at http://remysharp.com/2007/10/08/what-is-jsonp/

看看http://remysharp.com/2007/10/08/what-is-jsonp/

EDIT TO COMMENT:

编辑注释:

JSON is right an object normally starts with braces {}.

JSON是正确的,对象通常以大括号{}开头。

Demo JSON:

演示JSON:

{
    "myString": "myValue",
    "myOtherString": ["My", "Other", "Value", "This", "Is", "An", "Array"]
}

But using the same method as JSONP you can parse an string instead of the weird looking thing starting and ending with {}.

但是使用与JSONP相同的方法,您可以解析一个字符串,而不是以{}开头和结尾的看起来很奇怪的东西。

as myCallback in my example 1: myCallback("HERE I PASS A STRING INSTEAD OF AN OBJECT"). See the "". "STRING GOES IN HERE"

作为示例1中的myCallback: myCallback(“这里我传递一个字符串而不是一个对象”)。看到“”。“在这里”

if it was JSON and taken usage of my DEMO JSON it would look like this:

如果它是JSON并且使用了我的DEMO JSON它会是这样的:

myCallback({
    "myString": "myValue",
    "myOtherString": ["My", "Other", "Value", "This", "Is", "An", "Array"]
})

#2


1  

JS:

JS:

$.ajax({
    type: 'GET',
    url: 'curl.php',
    data: 'user_id=user_id', //assuming user_id value was already set.
    success: function(html)
    {
        $('#info').empty().html(html);
    }  
});

curl.php;

curl.php;

function get_data($url){
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
$user_id = $_GET['user_id'];
$url= "http://www.example.com/user.php?user_id=".$user_id;
echo get_data($url);

#3


0  

You can set up a proxy on your domain and use CURL to get the json from other domain. You then send request to this proxy.

您可以在域上设置一个代理,并使用CURL从其他域获取json。然后将请求发送到此代理。

Alternately you would need to set up the other domain to process request as jsonp in order to be able to access directly with ajax

或者,您需要设置另一个域来作为jsonp处理请求,以便能够使用ajax直接访问请求

#4


0  

You have to use JSONP or tell the website owner of the other site to add a Access-Control-Allow-Origin header.

您必须使用JSONP或通知另一个站点的网站所有者添加访问控制允许的源头。

#5


0  

I also get same problem when access cross domain using ajax. Then I solve it by make the ajax call to same domain. Then on that php script I apply following code block. This will get the content from cros domain and out put the same to ajax call.

使用ajax访问跨域时也会遇到同样的问题。然后我通过对同一个域进行ajax调用来解决这个问题。然后在这个php脚本上应用以下代码块。这将从cros域获取内容,并将其输出到ajax调用。


    $content = file_get_contents('http://crossdomain.com');
    echo $content;

#6


0  

This can be done through jsonp. Following is an example.

这可以通过jsonp实现。下面是一个例子。

$.ajax({
   url: "example.com/respond.php",
   data: {id: id},
   dataType: "jsonp",
   jsonp : "callback",
   jsonpCallback: "jsonpcallback"
}); 

respond.php

respond.php

<?php
header("content-type: application/json"); 

if (isset($_GET['id'])) $rtnjsonobj->id = $_GET['id']; 
$rtnjsonobj->message = "This is the message from cross domain";


echo $_GET['callback']. '('. json_encode($rtnjsonobj) . ')';    
?>

#1


2  

using javascript you can do the same as if it was JSON, it's called JSONP the P is with padding.

使用javascript,您可以像使用JSON那样做,它被称为JSONP, P带有填充。

Or you can call it JSON with callback:

或者你可以用回叫来调用JSON:

// Request Page

/ /请求页面

myCallback("Some string or Object to parse to your site");

// Your Page

/ /页面

window["myCallback"] = function(string_or_object) {
    // Here you can do everything with the parsed data
}

Create a script-tag and include the request page. Make sure to define your callback before including the script-tag

创建一个脚本标记并包含请求页面。在包含脚本标记之前,请确保定义回调

or you can use jQuery's ajax method with dataType set to jsonp:

或者您可以使用jQuery的ajax方法,将数据类型设置为jsonp:

$.ajax({
    "url": "requst_page.php",
    "dataType": "jsonp",
    "success": function(string_or_object) {
        // Here you can do everything with the parsed data
    }
})

Look at http://remysharp.com/2007/10/08/what-is-jsonp/

看看http://remysharp.com/2007/10/08/what-is-jsonp/

EDIT TO COMMENT:

编辑注释:

JSON is right an object normally starts with braces {}.

JSON是正确的,对象通常以大括号{}开头。

Demo JSON:

演示JSON:

{
    "myString": "myValue",
    "myOtherString": ["My", "Other", "Value", "This", "Is", "An", "Array"]
}

But using the same method as JSONP you can parse an string instead of the weird looking thing starting and ending with {}.

但是使用与JSONP相同的方法,您可以解析一个字符串,而不是以{}开头和结尾的看起来很奇怪的东西。

as myCallback in my example 1: myCallback("HERE I PASS A STRING INSTEAD OF AN OBJECT"). See the "". "STRING GOES IN HERE"

作为示例1中的myCallback: myCallback(“这里我传递一个字符串而不是一个对象”)。看到“”。“在这里”

if it was JSON and taken usage of my DEMO JSON it would look like this:

如果它是JSON并且使用了我的DEMO JSON它会是这样的:

myCallback({
    "myString": "myValue",
    "myOtherString": ["My", "Other", "Value", "This", "Is", "An", "Array"]
})

#2


1  

JS:

JS:

$.ajax({
    type: 'GET',
    url: 'curl.php',
    data: 'user_id=user_id', //assuming user_id value was already set.
    success: function(html)
    {
        $('#info').empty().html(html);
    }  
});

curl.php;

curl.php;

function get_data($url){
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
$user_id = $_GET['user_id'];
$url= "http://www.example.com/user.php?user_id=".$user_id;
echo get_data($url);

#3


0  

You can set up a proxy on your domain and use CURL to get the json from other domain. You then send request to this proxy.

您可以在域上设置一个代理,并使用CURL从其他域获取json。然后将请求发送到此代理。

Alternately you would need to set up the other domain to process request as jsonp in order to be able to access directly with ajax

或者,您需要设置另一个域来作为jsonp处理请求,以便能够使用ajax直接访问请求

#4


0  

You have to use JSONP or tell the website owner of the other site to add a Access-Control-Allow-Origin header.

您必须使用JSONP或通知另一个站点的网站所有者添加访问控制允许的源头。

#5


0  

I also get same problem when access cross domain using ajax. Then I solve it by make the ajax call to same domain. Then on that php script I apply following code block. This will get the content from cros domain and out put the same to ajax call.

使用ajax访问跨域时也会遇到同样的问题。然后我通过对同一个域进行ajax调用来解决这个问题。然后在这个php脚本上应用以下代码块。这将从cros域获取内容,并将其输出到ajax调用。


    $content = file_get_contents('http://crossdomain.com');
    echo $content;

#6


0  

This can be done through jsonp. Following is an example.

这可以通过jsonp实现。下面是一个例子。

$.ajax({
   url: "example.com/respond.php",
   data: {id: id},
   dataType: "jsonp",
   jsonp : "callback",
   jsonpCallback: "jsonpcallback"
}); 

respond.php

respond.php

<?php
header("content-type: application/json"); 

if (isset($_GET['id'])) $rtnjsonobj->id = $_GET['id']; 
$rtnjsonobj->message = "This is the message from cross domain";


echo $_GET['callback']. '('. json_encode($rtnjsonobj) . ')';    
?>