I am trying to access the JSON object from this third party service, inside my javascript application.
However, because I was getting the Same Origin Policy error earlier, I have tried using JSONP and jQuery and AJAX, respectively as follows.
Using JSONP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function foo(data)
{
// do stuff with JSON
console.log(data);
}
var script = document.createElement('script');
script.src = 'https://matchstat.com/tennis/match-stats/m/8348298?callback=foo'
document.body.appendChild(script);
// or document.head.appendChild(script) in modern browsers
</script>
</body>
</html>
Using jQuery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.min.js"></script>
<script>
$.getJSON("https://matchstat.com/tennis/match-stats/m/8348298?callback=?", function(data) {
console.log(data);
// Get the element with id summary and set the inner text to the result.
$('#summary').text(data.result);
});
</script>
</body>
</html>
Using AJAX
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.min.js"></script>
<script>
CallURL();
function CallURL(){
$.ajax({
url: 'https://matchstat.com/tennis/match-stats/m/8348298',
type: "GET",
dataType: "jsonp",
async:false,
success: function (msg) {
JsonpCallback(msg);
},
error: function () {
ErrorFunction();
}
});
}
function JsonpCallback(json)
{
console.log("came inside JsonpCallback ...");
document.getElementById('summary').innerHtml=json.result;
}
function ErrorFunction()
{
console.log("came inside the ErrorFunction() ...");
}
</script>
</body>
</html>
However, I haven't been able to access the JSON object.