how to use vue-resource and proxy.php to solve the cross domain problem
up vote
0
down vote
favorite
First of all. I want to take a few words to explain that I am fully aware of what Cross-domain problem is and how to deal with it (in plain js with jQuery, but not in vue)
Here is the case:
I want to get the WFS features (as xml) from some of the geoserver (other domains) using the HTTP GET request. It is clear that it will be blocked because of the same origin policy.
I used to use a very simple proxy.php file to go around this and it works pretty well.
The proxy.php file is like this:
<?php
$nix="";
$type=$_GET['requrl'];
if ($_GET['requrl'] != $nix)
$file = file_get_contents($_GET['requrl']);
else
$file = "false type";
echo $file;
?>
So basically I write in JS an Ajax-call with jQuery. which looks like this:
jQuery.ajax(
type: "GET",
data:
requrl: "www.example.com/WFS?service=wfs&request=GetCapabilities"
,
url: "proxy.php"
).done(function (response)
// handle the response text/xml
console.log(response);
)
The old way works well, I send the "true" url as requrl to the php file, the php get what I want for me and return it as response. So I can later hanlde the response with the jQuery-ajax.
The real problem:
But now I am moving my whole app to the vue.js framework. So I am now using vue-resource instead of the jQuery-ajax.
The vue-resource is not hard to understand. So I make the HTTP GET request as below:
this.$http.get('/static/proxy.php', params: requrl:"www.google.de").then(response =>
// success
console.log("oh! success!");
, response =>
// error
console.log("oh! error!");
);
I placed the proxy.php file in the public/static folder and the vue-resource keeps getting the content of the proxy.php for me. But not go through it and retrun me the response.
I have checked the HTTP GET request with the firefox dev tools and it shows me that the GET request is 200 OK. But the response is always the content of that proxy.php. It seems like the php file is not doing the work that I expected it to do.
And here is the response I got from the vue-resource GET request:
<?php $nix=""; $type=$_GET['requrl']; if ($_GET['requrl'] != $nix) $file = file_get_contents($_GET['requrl']); else $file = "false type"; echo $file; ?>
I am kind of knowing that the dev server could be the reason because in old days, I have php installed in my apache local server, and now with vue.js. I just type npm run serve each time I want to start a dev server. I don't know what kind of dev server I just started and if it works with php.
So I would like to ask how u guys deal with vue-resource and php. Or maybe there is a better way in vue.js to go around the cross domain problem?
Here is the dev environment I am using now:
The project is created with vue.js and vue/cli 3 (contained webpack and etc.)
The plug-in I use is vuetify and vue-resource
For those who maybe search for the same question in the future. I have solved my question this way:
Set up an apache server, by which php is installed (I got the content of my proxy.php because of that I didn't have php installed in the dev server, which is started by the command npm run serve, and that was why it didn't work!)
Make sure that you enable the CORS on your apache server! Because this apache server will run at an different port (for example 8888) and your dev server for your vue app will run for example at 8080 as default! And different ports will be considered also as Cross Domain! So make sure to enable the CORS on your apache server!
Point your HTTP GET Request to your proxy.php file in your apache server with vue-resource, here an example in vue app (my apache server is running at port 8888, the proxy.php file is also showed in this question, here I got my own ID with the GET Request to the url http://httpbin.org/ip):
this.$http.get('http://localhost:8888/proxy.php', params: requrl: "http://httpbin.org/ip").then(response =>
// success
console.log("oh! success!");
console.log("success response: ", response);
, response =>
// error
console.log("oh! error!")
console.log("error response: ", response);
);Now you successfully go around the Cross Domain!
php jquery vue.js vuejs2 vue-resource
|
show 8 more comments
up vote
0
down vote
favorite
First of all. I want to take a few words to explain that I am fully aware of what Cross-domain problem is and how to deal with it (in plain js with jQuery, but not in vue)
Here is the case:
I want to get the WFS features (as xml) from some of the geoserver (other domains) using the HTTP GET request. It is clear that it will be blocked because of the same origin policy.
I used to use a very simple proxy.php file to go around this and it works pretty well.
The proxy.php file is like this:
<?php
$nix="";
$type=$_GET['requrl'];
if ($_GET['requrl'] != $nix)
$file = file_get_contents($_GET['requrl']);
else
$file = "false type";
echo $file;
?>
So basically I write in JS an Ajax-call with jQuery. which looks like this:
jQuery.ajax(
type: "GET",
data:
requrl: "www.example.com/WFS?service=wfs&request=GetCapabilities"
,
url: "proxy.php"
).done(function (response)
// handle the response text/xml
console.log(response);
)
The old way works well, I send the "true" url as requrl to the php file, the php get what I want for me and return it as response. So I can later hanlde the response with the jQuery-ajax.
The real problem:
But now I am moving my whole app to the vue.js framework. So I am now using vue-resource instead of the jQuery-ajax.
The vue-resource is not hard to understand. So I make the HTTP GET request as below:
this.$http.get('/static/proxy.php', params: requrl:"www.google.de").then(response =>
// success
console.log("oh! success!");
, response =>
// error
console.log("oh! error!");
);
I placed the proxy.php file in the public/static folder and the vue-resource keeps getting the content of the proxy.php for me. But not go through it and retrun me the response.
I have checked the HTTP GET request with the firefox dev tools and it shows me that the GET request is 200 OK. But the response is always the content of that proxy.php. It seems like the php file is not doing the work that I expected it to do.
And here is the response I got from the vue-resource GET request:
<?php $nix=""; $type=$_GET['requrl']; if ($_GET['requrl'] != $nix) $file = file_get_contents($_GET['requrl']); else $file = "false type"; echo $file; ?>
I am kind of knowing that the dev server could be the reason because in old days, I have php installed in my apache local server, and now with vue.js. I just type npm run serve each time I want to start a dev server. I don't know what kind of dev server I just started and if it works with php.
So I would like to ask how u guys deal with vue-resource and php. Or maybe there is a better way in vue.js to go around the cross domain problem?
Here is the dev environment I am using now:
The project is created with vue.js and vue/cli 3 (contained webpack and etc.)
The plug-in I use is vuetify and vue-resource
For those who maybe search for the same question in the future. I have solved my question this way:
Set up an apache server, by which php is installed (I got the content of my proxy.php because of that I didn't have php installed in the dev server, which is started by the command npm run serve, and that was why it didn't work!)
Make sure that you enable the CORS on your apache server! Because this apache server will run at an different port (for example 8888) and your dev server for your vue app will run for example at 8080 as default! And different ports will be considered also as Cross Domain! So make sure to enable the CORS on your apache server!
Point your HTTP GET Request to your proxy.php file in your apache server with vue-resource, here an example in vue app (my apache server is running at port 8888, the proxy.php file is also showed in this question, here I got my own ID with the GET Request to the url http://httpbin.org/ip):
this.$http.get('http://localhost:8888/proxy.php', params: requrl: "http://httpbin.org/ip").then(response =>
// success
console.log("oh! success!");
console.log("success response: ", response);
, response =>
// error
console.log("oh! error!")
console.log("error response: ", response);
);Now you successfully go around the Cross Domain!
php jquery vue.js vuejs2 vue-resource
Have you tried dumping $_REQUEST just to make sure that $_GET has not been unset somehow, I have experienced this happen once in the past where $_GET is cleared out because someone made a mistake with a XSS prevention parser..
– Marc Newton
Nov 10 at 14:26
@Marc Newton Thanks for your advise but I don't really think this is the problem here. Can I ask how you go around with the cross domain problem?
– Min XIE
Nov 10 at 14:34
Is the console printing you an error? Can we see it?
– Angel Roma
Nov 10 at 14:38
1
If your seeing the source code for proxy.php then you not installed or setup php correctly. alsonpm run serve
wont start php-fpm will it?.
– Lawrence Cherone
Nov 10 at 15:07
1
Oh yeah, so you are getting your proxy.php a text response and you are right, your server won't deal with php because it's designed to work with your vue files only.
– Angel Roma
Nov 10 at 15:12
|
show 8 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
First of all. I want to take a few words to explain that I am fully aware of what Cross-domain problem is and how to deal with it (in plain js with jQuery, but not in vue)
Here is the case:
I want to get the WFS features (as xml) from some of the geoserver (other domains) using the HTTP GET request. It is clear that it will be blocked because of the same origin policy.
I used to use a very simple proxy.php file to go around this and it works pretty well.
The proxy.php file is like this:
<?php
$nix="";
$type=$_GET['requrl'];
if ($_GET['requrl'] != $nix)
$file = file_get_contents($_GET['requrl']);
else
$file = "false type";
echo $file;
?>
So basically I write in JS an Ajax-call with jQuery. which looks like this:
jQuery.ajax(
type: "GET",
data:
requrl: "www.example.com/WFS?service=wfs&request=GetCapabilities"
,
url: "proxy.php"
).done(function (response)
// handle the response text/xml
console.log(response);
)
The old way works well, I send the "true" url as requrl to the php file, the php get what I want for me and return it as response. So I can later hanlde the response with the jQuery-ajax.
The real problem:
But now I am moving my whole app to the vue.js framework. So I am now using vue-resource instead of the jQuery-ajax.
The vue-resource is not hard to understand. So I make the HTTP GET request as below:
this.$http.get('/static/proxy.php', params: requrl:"www.google.de").then(response =>
// success
console.log("oh! success!");
, response =>
// error
console.log("oh! error!");
);
I placed the proxy.php file in the public/static folder and the vue-resource keeps getting the content of the proxy.php for me. But not go through it and retrun me the response.
I have checked the HTTP GET request with the firefox dev tools and it shows me that the GET request is 200 OK. But the response is always the content of that proxy.php. It seems like the php file is not doing the work that I expected it to do.
And here is the response I got from the vue-resource GET request:
<?php $nix=""; $type=$_GET['requrl']; if ($_GET['requrl'] != $nix) $file = file_get_contents($_GET['requrl']); else $file = "false type"; echo $file; ?>
I am kind of knowing that the dev server could be the reason because in old days, I have php installed in my apache local server, and now with vue.js. I just type npm run serve each time I want to start a dev server. I don't know what kind of dev server I just started and if it works with php.
So I would like to ask how u guys deal with vue-resource and php. Or maybe there is a better way in vue.js to go around the cross domain problem?
Here is the dev environment I am using now:
The project is created with vue.js and vue/cli 3 (contained webpack and etc.)
The plug-in I use is vuetify and vue-resource
For those who maybe search for the same question in the future. I have solved my question this way:
Set up an apache server, by which php is installed (I got the content of my proxy.php because of that I didn't have php installed in the dev server, which is started by the command npm run serve, and that was why it didn't work!)
Make sure that you enable the CORS on your apache server! Because this apache server will run at an different port (for example 8888) and your dev server for your vue app will run for example at 8080 as default! And different ports will be considered also as Cross Domain! So make sure to enable the CORS on your apache server!
Point your HTTP GET Request to your proxy.php file in your apache server with vue-resource, here an example in vue app (my apache server is running at port 8888, the proxy.php file is also showed in this question, here I got my own ID with the GET Request to the url http://httpbin.org/ip):
this.$http.get('http://localhost:8888/proxy.php', params: requrl: "http://httpbin.org/ip").then(response =>
// success
console.log("oh! success!");
console.log("success response: ", response);
, response =>
// error
console.log("oh! error!")
console.log("error response: ", response);
);Now you successfully go around the Cross Domain!
php jquery vue.js vuejs2 vue-resource
First of all. I want to take a few words to explain that I am fully aware of what Cross-domain problem is and how to deal with it (in plain js with jQuery, but not in vue)
Here is the case:
I want to get the WFS features (as xml) from some of the geoserver (other domains) using the HTTP GET request. It is clear that it will be blocked because of the same origin policy.
I used to use a very simple proxy.php file to go around this and it works pretty well.
The proxy.php file is like this:
<?php
$nix="";
$type=$_GET['requrl'];
if ($_GET['requrl'] != $nix)
$file = file_get_contents($_GET['requrl']);
else
$file = "false type";
echo $file;
?>
So basically I write in JS an Ajax-call with jQuery. which looks like this:
jQuery.ajax(
type: "GET",
data:
requrl: "www.example.com/WFS?service=wfs&request=GetCapabilities"
,
url: "proxy.php"
).done(function (response)
// handle the response text/xml
console.log(response);
)
The old way works well, I send the "true" url as requrl to the php file, the php get what I want for me and return it as response. So I can later hanlde the response with the jQuery-ajax.
The real problem:
But now I am moving my whole app to the vue.js framework. So I am now using vue-resource instead of the jQuery-ajax.
The vue-resource is not hard to understand. So I make the HTTP GET request as below:
this.$http.get('/static/proxy.php', params: requrl:"www.google.de").then(response =>
// success
console.log("oh! success!");
, response =>
// error
console.log("oh! error!");
);
I placed the proxy.php file in the public/static folder and the vue-resource keeps getting the content of the proxy.php for me. But not go through it and retrun me the response.
I have checked the HTTP GET request with the firefox dev tools and it shows me that the GET request is 200 OK. But the response is always the content of that proxy.php. It seems like the php file is not doing the work that I expected it to do.
And here is the response I got from the vue-resource GET request:
<?php $nix=""; $type=$_GET['requrl']; if ($_GET['requrl'] != $nix) $file = file_get_contents($_GET['requrl']); else $file = "false type"; echo $file; ?>
I am kind of knowing that the dev server could be the reason because in old days, I have php installed in my apache local server, and now with vue.js. I just type npm run serve each time I want to start a dev server. I don't know what kind of dev server I just started and if it works with php.
So I would like to ask how u guys deal with vue-resource and php. Or maybe there is a better way in vue.js to go around the cross domain problem?
Here is the dev environment I am using now:
The project is created with vue.js and vue/cli 3 (contained webpack and etc.)
The plug-in I use is vuetify and vue-resource
For those who maybe search for the same question in the future. I have solved my question this way:
Set up an apache server, by which php is installed (I got the content of my proxy.php because of that I didn't have php installed in the dev server, which is started by the command npm run serve, and that was why it didn't work!)
Make sure that you enable the CORS on your apache server! Because this apache server will run at an different port (for example 8888) and your dev server for your vue app will run for example at 8080 as default! And different ports will be considered also as Cross Domain! So make sure to enable the CORS on your apache server!
Point your HTTP GET Request to your proxy.php file in your apache server with vue-resource, here an example in vue app (my apache server is running at port 8888, the proxy.php file is also showed in this question, here I got my own ID with the GET Request to the url http://httpbin.org/ip):
this.$http.get('http://localhost:8888/proxy.php', params: requrl: "http://httpbin.org/ip").then(response =>
// success
console.log("oh! success!");
console.log("success response: ", response);
, response =>
// error
console.log("oh! error!")
console.log("error response: ", response);
);Now you successfully go around the Cross Domain!
php jquery vue.js vuejs2 vue-resource
php jquery vue.js vuejs2 vue-resource
edited Nov 11 at 12:30
asked Nov 10 at 14:14
Min XIE
517
517
Have you tried dumping $_REQUEST just to make sure that $_GET has not been unset somehow, I have experienced this happen once in the past where $_GET is cleared out because someone made a mistake with a XSS prevention parser..
– Marc Newton
Nov 10 at 14:26
@Marc Newton Thanks for your advise but I don't really think this is the problem here. Can I ask how you go around with the cross domain problem?
– Min XIE
Nov 10 at 14:34
Is the console printing you an error? Can we see it?
– Angel Roma
Nov 10 at 14:38
1
If your seeing the source code for proxy.php then you not installed or setup php correctly. alsonpm run serve
wont start php-fpm will it?.
– Lawrence Cherone
Nov 10 at 15:07
1
Oh yeah, so you are getting your proxy.php a text response and you are right, your server won't deal with php because it's designed to work with your vue files only.
– Angel Roma
Nov 10 at 15:12
|
show 8 more comments
Have you tried dumping $_REQUEST just to make sure that $_GET has not been unset somehow, I have experienced this happen once in the past where $_GET is cleared out because someone made a mistake with a XSS prevention parser..
– Marc Newton
Nov 10 at 14:26
@Marc Newton Thanks for your advise but I don't really think this is the problem here. Can I ask how you go around with the cross domain problem?
– Min XIE
Nov 10 at 14:34
Is the console printing you an error? Can we see it?
– Angel Roma
Nov 10 at 14:38
1
If your seeing the source code for proxy.php then you not installed or setup php correctly. alsonpm run serve
wont start php-fpm will it?.
– Lawrence Cherone
Nov 10 at 15:07
1
Oh yeah, so you are getting your proxy.php a text response and you are right, your server won't deal with php because it's designed to work with your vue files only.
– Angel Roma
Nov 10 at 15:12
Have you tried dumping $_REQUEST just to make sure that $_GET has not been unset somehow, I have experienced this happen once in the past where $_GET is cleared out because someone made a mistake with a XSS prevention parser..
– Marc Newton
Nov 10 at 14:26
Have you tried dumping $_REQUEST just to make sure that $_GET has not been unset somehow, I have experienced this happen once in the past where $_GET is cleared out because someone made a mistake with a XSS prevention parser..
– Marc Newton
Nov 10 at 14:26
@Marc Newton Thanks for your advise but I don't really think this is the problem here. Can I ask how you go around with the cross domain problem?
– Min XIE
Nov 10 at 14:34
@Marc Newton Thanks for your advise but I don't really think this is the problem here. Can I ask how you go around with the cross domain problem?
– Min XIE
Nov 10 at 14:34
Is the console printing you an error? Can we see it?
– Angel Roma
Nov 10 at 14:38
Is the console printing you an error? Can we see it?
– Angel Roma
Nov 10 at 14:38
1
1
If your seeing the source code for proxy.php then you not installed or setup php correctly. also
npm run serve
wont start php-fpm will it?.– Lawrence Cherone
Nov 10 at 15:07
If your seeing the source code for proxy.php then you not installed or setup php correctly. also
npm run serve
wont start php-fpm will it?.– Lawrence Cherone
Nov 10 at 15:07
1
1
Oh yeah, so you are getting your proxy.php a text response and you are right, your server won't deal with php because it's designed to work with your vue files only.
– Angel Roma
Nov 10 at 15:12
Oh yeah, so you are getting your proxy.php a text response and you are right, your server won't deal with php because it's designed to work with your vue files only.
– Angel Roma
Nov 10 at 15:12
|
show 8 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I placed the proxy.php file in the public/static folder and the vue-resource keeps getting the content of the proxy.php for me. But not go through it and return me the response.
In order to run your php files you will need to configure a local server to serve and execute php files because your npm run serve
command is serving static files only. (Javascript, html, css and etc)
I suggest you to install a Xampp to easily configure a PHP development environment.
So you will get a local server for your php environment and another for your vue app running in different ports.
Than you very much! I followed your suggestion and configured a local apache server to run my proxy.php. So I can reach to my own apache server and use the php file! I now "again" go around the Cross Domain problem and I also put my solution in the question for other people who may has the same question I had.
– Min XIE
Nov 11 at 12:32
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I placed the proxy.php file in the public/static folder and the vue-resource keeps getting the content of the proxy.php for me. But not go through it and return me the response.
In order to run your php files you will need to configure a local server to serve and execute php files because your npm run serve
command is serving static files only. (Javascript, html, css and etc)
I suggest you to install a Xampp to easily configure a PHP development environment.
So you will get a local server for your php environment and another for your vue app running in different ports.
Than you very much! I followed your suggestion and configured a local apache server to run my proxy.php. So I can reach to my own apache server and use the php file! I now "again" go around the Cross Domain problem and I also put my solution in the question for other people who may has the same question I had.
– Min XIE
Nov 11 at 12:32
add a comment |
up vote
1
down vote
accepted
I placed the proxy.php file in the public/static folder and the vue-resource keeps getting the content of the proxy.php for me. But not go through it and return me the response.
In order to run your php files you will need to configure a local server to serve and execute php files because your npm run serve
command is serving static files only. (Javascript, html, css and etc)
I suggest you to install a Xampp to easily configure a PHP development environment.
So you will get a local server for your php environment and another for your vue app running in different ports.
Than you very much! I followed your suggestion and configured a local apache server to run my proxy.php. So I can reach to my own apache server and use the php file! I now "again" go around the Cross Domain problem and I also put my solution in the question for other people who may has the same question I had.
– Min XIE
Nov 11 at 12:32
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I placed the proxy.php file in the public/static folder and the vue-resource keeps getting the content of the proxy.php for me. But not go through it and return me the response.
In order to run your php files you will need to configure a local server to serve and execute php files because your npm run serve
command is serving static files only. (Javascript, html, css and etc)
I suggest you to install a Xampp to easily configure a PHP development environment.
So you will get a local server for your php environment and another for your vue app running in different ports.
I placed the proxy.php file in the public/static folder and the vue-resource keeps getting the content of the proxy.php for me. But not go through it and return me the response.
In order to run your php files you will need to configure a local server to serve and execute php files because your npm run serve
command is serving static files only. (Javascript, html, css and etc)
I suggest you to install a Xampp to easily configure a PHP development environment.
So you will get a local server for your php environment and another for your vue app running in different ports.
answered Nov 10 at 15:54
Angel Roma
12915
12915
Than you very much! I followed your suggestion and configured a local apache server to run my proxy.php. So I can reach to my own apache server and use the php file! I now "again" go around the Cross Domain problem and I also put my solution in the question for other people who may has the same question I had.
– Min XIE
Nov 11 at 12:32
add a comment |
Than you very much! I followed your suggestion and configured a local apache server to run my proxy.php. So I can reach to my own apache server and use the php file! I now "again" go around the Cross Domain problem and I also put my solution in the question for other people who may has the same question I had.
– Min XIE
Nov 11 at 12:32
Than you very much! I followed your suggestion and configured a local apache server to run my proxy.php. So I can reach to my own apache server and use the php file! I now "again" go around the Cross Domain problem and I also put my solution in the question for other people who may has the same question I had.
– Min XIE
Nov 11 at 12:32
Than you very much! I followed your suggestion and configured a local apache server to run my proxy.php. So I can reach to my own apache server and use the php file! I now "again" go around the Cross Domain problem and I also put my solution in the question for other people who may has the same question I had.
– Min XIE
Nov 11 at 12:32
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239824%2fhow-to-use-vue-resource-and-proxy-php-to-solve-the-cross-domain-problem%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you tried dumping $_REQUEST just to make sure that $_GET has not been unset somehow, I have experienced this happen once in the past where $_GET is cleared out because someone made a mistake with a XSS prevention parser..
– Marc Newton
Nov 10 at 14:26
@Marc Newton Thanks for your advise but I don't really think this is the problem here. Can I ask how you go around with the cross domain problem?
– Min XIE
Nov 10 at 14:34
Is the console printing you an error? Can we see it?
– Angel Roma
Nov 10 at 14:38
1
If your seeing the source code for proxy.php then you not installed or setup php correctly. also
npm run serve
wont start php-fpm will it?.– Lawrence Cherone
Nov 10 at 15:07
1
Oh yeah, so you are getting your proxy.php a text response and you are right, your server won't deal with php because it's designed to work with your vue files only.
– Angel Roma
Nov 10 at 15:12