I’m trying to get JSON data from a website using the following code, but I get the error Uncaught SyntaxError: Unexpected identifier at file:///android_asset/www/projectName.html:1
If I save the data from the website as a .json file I can access it fine, but not if I access it from the website. How do I make this work from the website?
HTML:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.get('http://example.ca/log_sheets.json', function(data) {
alert('get performed');
var obj = eval ("(" + data + ")");
$("p").html(obj[0].log_sheet.activity_type);
});
});
});
</script>
</head>
<body>
<h2>Heading</h2>
<p>Display</p>
<button>Click me</button>
</body>
</html>
Sample of JSON data:
[{"log_sheet":{"activity_type":"Normal","activity_value":null,"carbs_value":25.0, ect... }}]
Code to run html page with phonegap:
package com.example.projectName;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import org.apache.cordova.*;
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/projectName.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
If jQuery thinks you’re retrieving JSON (via server-provided mime-type, a final argument of
$.get(...,'json'), or by using$.getJSON), the data passed to the callback will already be a JavaScript object.In that case, you won’t want to
evalit.