Question
I'm developing a Tizen web application that needs to connect to a PHP file on a Linux server. While the application works fine on my local server and when accessing the PHP file directly through a browser (http://115.xx.xxx.xxx/time.php), it fails to connect when running on a Tizen device (Z3).
I've already added the necessary policies in config.xml following the documentation, but the connection still doesn't work. The application only shows an alert but fails to connect to the server.
Here are the relevant code snippets:
index.html
<!DOCTYPE html>
<html>
<head>
<script src="main.js"></script>
</head>
<body>
<a href="115.XX.XXX.XXX">Open Linux Server</a>
</body>
</html>
main.js
window.onload = function() {
alert("Running");
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://115.xx.xxx.xxx/time.php', true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
}
};
xhr.send();
};
config.xml
<access origin="*" subdomains="true"/>
<access origin="http://115.xx.xxx.xxx" subdomains="true"/>
<tizen:privilege name="http://tizen.org/privilege/internet"/>
When clicking the "Open Linux Server" link, I get a "This webpage is not available" error, though the direct link to time.php works in a browser. How can I resolve this server connection issue?
Answer
Problem Understanding
The issue appears to be related to Cross-Origin Resource Sharing (CORS) and proper configuration of network access in the Tizen web application. While the basic setup seems correct, there are several potential areas that need verification.
Solution Methods
-
Verify CORS Configuration:
- Ensure your PHP file includes the proper CORS headers:
<?php header("Access-Control-Allow-Origin: *"); ?>
- Ensure your PHP file includes the proper CORS headers:
-
Update XMLHttpRequest Configuration:
- Use double quotes for consistency:
xhr.open("GET", "http://115.xx.xxx.xxx/time.php", true);
- Use double quotes for consistency:
-
Debugging Steps:
- Run your application in debug mode ("Debug As > Tizen Web Application")
- Check the Web Inspector console for errors
- Monitor the Network tab to inspect request/response details
-
Alternative Connection Method:
- Try using the fetch API instead of XMLHttpRequest:
fetch('http://115.xx.xxx.xxx/time.php') .then(response => response.text()) .then(data => alert(data)) .catch(error => console.error('Error:', error));
- Try using the fetch API instead of XMLHttpRequest:
Additional Tips
- Make sure your Linux server is accessible from the Tizen device's network
- Verify that no firewalls are blocking the connection
- Consider using HTTPS instead of HTTP if possible
- Test with a simpler PHP file that returns plain text to isolate the issue