Question
I'm developing a Tizen web application and trying to send requests to my server using XMLHttpRequest, but it keeps failing. I've already included jQuery, AngularJS, TAU, and other required libraries in my project. What could be causing this issue?
Here's my current implementation:
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
// Success case
} else {
// Error case
}
};
xhr.send();
Answer
Problem Understanding
The issue likely stems from missing CORS (Cross-Origin Resource Sharing) permissions in your Tizen application's configuration. Tizen web applications require explicit permission to access external resources.
Solution Methods
-
Configure config.xml for Network Access:
- Add the necessary access permissions for your domains
- Include the internet feature requirement
-
Handle JSON Response Properly:
- Consider parsing the response as text first, then converting to JSON
- Implement proper error handling
Code Examples
- config.xml configuration:
<access origin="http://yourdomain.com" subdomains="true"></access>
<access origin="*" subdomains="true"></access>
<feature name="http://tizen.org/feature/network.internet"/>
- Modified XMLHttpRequest implementation:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
try {
var data = JSON.parse(xhr.responseText);
// Process your data here
} catch (e) {
console.error("Error parsing JSON:", e);
}
}
};
xhr.open("GET", url, true);
xhr.send();
Additional Tips
- For TAU UI integration, consider using data binding frameworks like AngularJS or jQuery to display your JSON data
- Always test your application with different network conditions
- Consider using modern alternatives like Fetch API if your Tizen version supports it
- For detailed TAU implementation questions, create a separate thread with specific requirements