Jump to content


Photo

Cross Origin Resource Sharing


  • Please log in to reply
10 replies to this topic

#1 locohost

locohost

    GMC Member

  • GMC Member
  • 459 posts
  • Version:GM:Studio

Posted 05 January 2012 - 02:10 AM

From what I'm reading you only have to add a header to your PHP script to enable CORS and call PHP scripts from outside the domain. I added this header to my PHP script. It's the first header added...

header('Access-Control-Allow-Origin: *');

However, my javascript extension always returns 404. I can enter the url in the browser and it works fine. The XmlHttpRequest still can't go outside the domain.

Are there more steps than just the PHP header? Can you explain how you got this working on your server?

Thanks guys :smile:



  • 0

#2 beatson

beatson

    GMC Member

  • GMC Member
  • 319 posts
  • Version:GM8

Posted 05 January 2012 - 04:31 PM

From what I'm reading you only have to add a header to your PHP script to enable CORS and call PHP scripts from outside the domain. I added this header to my PHP script. It's the first header added...

header('Access-Control-Allow-Origin: *');

However, my javascript extension always returns 404. I can enter the url in the browser and it works fine. The XmlHttpRequest still can't go outside the domain.

Are there more steps than just the PHP header? Can you explain how you got this working on your server?

Thanks guys :smile:


Is it at the top of the file?
  • 0

#3 locohost

locohost

    GMC Member

  • GMC Member
  • 459 posts
  • Version:GM:Studio

Posted 05 January 2012 - 05:31 PM


From what I'm reading you only have to add a header to your PHP script to enable CORS and call PHP scripts from outside the domain. I added this header to my PHP script. It's the first header added...

header('Access-Control-Allow-Origin: *');

However, my javascript extension always returns 404. I can enter the url in the browser and it works fine. The XmlHttpRequest still can't go outside the domain.

Are there more steps than just the PHP header? Can you explain how you got this working on your server?

Thanks guys Posted Image


Is it at the top of the file?



Yep, very first line, first header added. Do you have this working with only adding the header to the PHP scripts? Are you running Apache2 web server?
  • 0

#4 beatson

beatson

    GMC Member

  • GMC Member
  • 319 posts
  • Version:GM8

Posted 05 January 2012 - 06:37 PM



From what I'm reading you only have to add a header to your PHP script to enable CORS and call PHP scripts from outside the domain. I added this header to my PHP script. It's the first header added...

header('Access-Control-Allow-Origin: *');

However, my javascript extension always returns 404. I can enter the url in the browser and it works fine. The XmlHttpRequest still can't go outside the domain.

Are there more steps than just the PHP header? Can you explain how you got this working on your server?

Thanks guys Posted Image


Is it at the top of the file?



Yep, very first line, first header added. Do you have this working with only adding the header to the PHP scripts? Are you running Apache2 web server?

Yep. That's all I had to put and everything worked. I've done a quick search online and haven't found anything that could help you so I'm unsure as to what's causing this.
  • 1

#5 locohost

locohost

    GMC Member

  • GMC Member
  • 459 posts
  • Version:GM:Studio

Posted 05 January 2012 - 06:44 PM




From what I'm reading you only have to add a header to your PHP script to enable CORS and call PHP scripts from outside the domain. I added this header to my PHP script. It's the first header added...

header('Access-Control-Allow-Origin: *');

However, my javascript extension always returns 404. I can enter the url in the browser and it works fine. The XmlHttpRequest still can't go outside the domain.

Are there more steps than just the PHP header? Can you explain how you got this working on your server?

Thanks guys Posted Image


Is it at the top of the file?



Yep, very first line, first header added. Do you have this working with only adding the header to the PHP scripts? Are you running Apache2 web server?

Yep. That's all I had to put and everything worked. I've done a quick search online and haven't found anything that could help you so I'm unsure as to what's causing this.


Ok, I guess that's a bit encouraging. I don't need this immediately but it would be nice to have working in the next month or two.


Thanks Beatson Posted Image

Edited by locohost, 05 January 2012 - 06:46 PM.

  • 0

#6 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9413 posts
  • Version:Unknown

Posted 10 January 2012 - 10:14 AM

First, make sure you're not using Opera or IE. Opera refuses to allow it even with the header, and IE has been dodgy on that front.

Second, check the javascript console to make sure it's reporting that the cross domain request is not allowed. If it's just a 404, that's a different problem.

Thirdly, check your URL. Make sure you get small things right, ie http://www.example.com is not the same as http://example.com.
  • 1

#7 locohost

locohost

    GMC Member

  • GMC Member
  • 459 posts
  • Version:GM:Studio

Posted 10 January 2012 - 01:38 PM

First, make sure you're not using Opera or IE. Opera refuses to allow it even with the header, and IE has been dodgy on that front.

Second, check the javascript console to make sure it's reporting that the cross domain request is not allowed. If it's just a 404, that's a different problem.

Thirdly, check your URL. Make sure you get small things right, ie http://www.example.com is not the same as http://example.com.


The web server is LAMP box in my basement. I use DynDNS to forward a Url. Do I need to set something up in Apache2 so it knows what it's Url is? I'm reaching Posted Image
  • 0

#8 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9413 posts
  • Version:Unknown

Posted 11 January 2012 - 12:29 AM

As I said, check the exact wording of the error in the JS console to check exactly what the problem is.

Can you reach the page you're trying to pull in your browser?
  • 0

#9 locohost

locohost

    GMC Member

  • GMC Member
  • 459 posts
  • Version:GM:Studio

Posted 11 January 2012 - 12:59 AM

As I said, check the exact wording of the error in the JS console to check exactly what the problem is.

Can you reach the page you're trying to pull in your browser?



Yes, it works correctly from the browser. That's in the original post Posted Image

What is the "JS console"?

Edited by locohost, 11 January 2012 - 01:01 AM.

  • 0

#10 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9413 posts
  • Version:Unknown

Posted 11 January 2012 - 02:56 AM

The JavaScript Console shows any errors in the JavaScript. If the problem is the cross domain origin, it will say so in the console. If it's something else, it will say what it is.

On Chrome, you can find the JS Console under the wrench -> Tools -> JavaScript Console

It's really difficult to try to work out what your problem is without being able to check things myself. Do you happen to have some sort of web server you can upload your files to, so I can see both the game that isn't working, and the output of the PHP script? It may be a good idea to try a separate server anyway, to see if it's a problem with your server rather than your code. http://www.freehostia.com is a free host that allows you to use PHP/MySQL.
  • 1

#11 locohost

locohost

    GMC Member

  • GMC Member
  • 459 posts
  • Version:GM:Studio

Posted 11 January 2012 - 01:26 PM

The JavaScript Console shows any errors in the JavaScript. If the problem is the cross domain origin, it will say so in the console. If it's something else, it will say what it is.

On Chrome, you can find the JS Console under the wrench -> Tools -> JavaScript Console

It's really difficult to try to work out what your problem is without being able to check things myself. Do you happen to have some sort of web server you can upload your files to, so I can see both the game that isn't working, and the output of the PHP script? It may be a good idea to try a separate server anyway, to see if it's a problem with your server rather than your code. http://www.freehostia.com is a free host that allows you to use PHP/MySQL.




I found the js console in Chrome last night. I'll re-hook the game to the external web server this evening and see what errors I get.


Thanks for all your advice Dangerous_Dave. I really appreciate it.

Posted Image
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users