0

I have made a very simple index.php code as following

<!DOCTYPE html> 
<head> 
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/httpGet.js"></script>
<script type="text/javascript" src="js/jquery.mobile.js"></script>   

</head> 

<body>
 <input id="string" type="text"/>
 <input id="button" type= "button" value="Check" />

</body>
</html>

In this HTML code we get a number fro user and call httpgGet.js file. And the httpGet.js is :

$('#button').click(function() {
        var string = $('#string').val();
    $.post('http://...../isprime.php',{number: string},function(data){
      alert(data);
            } 
     }

we send the user number to a isprime.php file where it checks if a user number is a prime number or not. When I say $.post('php/isprime.php',.....) it works very well, but when I want to call the isprime.php from another computer, it does not work.

I mean $.post('192.168.1.1/test/isprime.php'....) gives me a not found error.

The file exists and I am able to run the code using direct call from browser. When I type in browser 192.168.1.1/test/isprime.php the file is reachable, but from the js file, it fails. Obviously, I can ping 192.168.1.1 too. I checked the code even with FireBug and no way thorough.

I searched a lot and came a cross that Browsers do not allow cross domain call (for security purpose, I guess), but I cannot find a solution to get rid of this problem. There are something about YQL (which is a query language and I don't know if I can use it or not). Also I saw something about cULR but I am not sure if that works too.

Kindly help. I really need it. thank u very much.

Espanta
  • 1,080
  • 1
  • 17
  • 27
  • 1
    first when using full URL use http:// scheme, and second: you have to enable [CORS](http://www.w3.org/TR/cors/) for your site –  Feb 16 '13 at 19:00
  • Hi @Akam and thanks for reply. I used http too and it failed. I am using a XAMPP web server on my second computer to play the role of server. Eventually, this php code will be hosted on an Android mobile device which is playing a web server such as PAW. Does enabling CORS help? – Espanta Feb 17 '13 at 03:06

1 Answers1

0

The most obvious solution, in my opinion, would be to make the request on the server side where you do not have the cross-domain request restrictions using the cURL library (as you mentioned, yes it would work). Of course, you would need to have the cURL library installed on your server. The PHP cURL manual should have enough information to get you started.

Default
  • 16,020
  • 3
  • 24
  • 38
  • Thank @Default, my problem is how to access the server and run a code there. Because cURL is a PhP library and PhP cannot be executed in mobile side. If I can access server to run PhP m problem is solved. I am a beginner so I may not know basics. Pls don't hesitate to tell me all basics. – Espanta Feb 17 '13 at 03:27
  • Right, so if you make an AJAX request to some endpoint on your server (as you are already doing via jQuery#post), you can use cURL functions to make a cross domain request and then return the information you need back to the client. The functions you will want to look at are curl_init(), curl_setopt(), curl_exec(), and curl_close() (possibly curl_getinfo() as well). Hopefully that helps. – Default Feb 17 '13 at 21:31