enigmatico

phaser - mighty - ajax-php-mysql

Recommended Posts

Hi, I want to connect with a database to save the scores, but, on first, I'm making a test.

I'm trying with this sample code to know if I could receive simply "text" request,

var pause_ajax = this.game.add.text(this.game.world.centerX, this.game.world.centerY+30, "connection");
        pause_ajax.inputEnabled = true;
        pause_ajax.events.onInputUp.add(function () {

                  var ajaxUrl = "http://www.anotherurlnotinmighty.es/php.php";

                  $.ajax({
                    url: ajaxUrl,
                    
                    dataType: "html",
                    method: "post",
                    beforeSend: function() {

                    },
                    success: function() {
                        alert("work");
                    },
                    error: function(){
                        alert("error")
                    }
                  });

        });

The php file is on another server. When I click the label, this makes the connection but it don't recive anything. ResponseText=""

 

php.php

<?php
session_start();
print "test";

?>

 

jQuery is correctly imported on my project.

 

There are any other best option to save the scores on dataBase?

 

Thanks

Share this post


Link to post
Share on other sites

Hi,

there are 2 options: 
1) set header to allow mightyeditor.mightyfingers.com access www.anotherurlnotinmighty.es
add the header("Access-Control-Allow-Origin: http://mightyeditor.mightyfingers.com") to your script:

<?php
header("Access-Control-Allow-Origin: http://mightyeditor.mightyfingers.com");
session_start();
print "test";

more info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

2) use jsonp - requires a little more scripting: http://www.sitepoint.com/jsonp-examples/

Share this post


Link to post
Share on other sites
On 28/2/2016 at 1:00 PM, stauzs said:

Hi,

there are 2 options: 
1) set header to allow mightyeditor.mightyfingers.com access www.anotherurlnotinmighty.es
add the header("Access-Control-Allow-Origin: http://mightyeditor.mightyfingers.com") to your script:


<?php
header("Access-Control-Allow-Origin: http://mightyeditor.mightyfingers.com");
session_start();
print "test";

more info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

2) use jsonp - requires a little more scripting: http://www.sitepoint.com/jsonp-examples/

I've used the option 1, it works! Thanks!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.