1

I am trying to read data from a JSON data by jQuery. Bus for some reasons it deosnt work.

Here is my JSON file: https://mersadesign.com/myPlayList.json and this is my code to get data:

$.getJSON("https://mersadesign.com/myPlayList.json", function(data){

  $.each(data.PlayListArray, function(key, val){
     alert(val.URL);
  });
});

Here is the demo:http://jsfiddle.net/SVk77/

Any idea to fix it?

10
  • it is a same origin policy violation - the external resource need to support either jsonp or CORS Commented Jun 25, 2014 at 3:07
  • see your browser console XMLHttpRequest cannot load https://mersadesign.com/myPlayList.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access. Commented Jun 25, 2014 at 3:08
  • @ArunPJohny So how can I make it working? some websites like fb provide data by JSON. I can get data from those servers with no problem. Commented Jun 25, 2014 at 3:10
  • that is what I pointed out in the first comment.. the resource you are trying to access must support CORS/jsonp Commented Jun 25, 2014 at 3:13
  • Try to get data on server side ansd send it to front end. Commented Jun 25, 2014 at 3:14

2 Answers 2

2

you can create web service for getting all music urls

PHP code:

<?php
    header('content-type: application/json; charset=utf-8');
    header("access-control-allow-origin: *");

$array = array("https://soundcloud.com/danial-sabagh/mane", "https://soundcloud.com/ajamband/gole-iran", "https://soundcloud.com/bibakofficial/kooch", "https://soundcloud.com/bibakofficial/mohammad-bibak-in-niz-bogzarad","https://soundcloud.com/kaishakhay/whine-and-kotch-j-chapri-f","https://soundcloud.com/amirtataloo/merci","https://soundcloud.com/amirtataloo/bikhiyal");// you can also apply your business logic and get url from database

echo json_encode(array("PlayListArray"=>$array));
    return;
?>

JQuery code for calling & getting response from php web service

Javascript code:

$.ajax({
      url: 'getMusicURL.php',
      type: "GET",
      dataType:'json',
      success:function(data){

    console.log(data);//using object data you access all music array 

             for(var i=0;i<data.PlayListArray.length;i++){
          console.log(data.PlayListArray[i]);
     }
}
});
Sign up to request clarification or add additional context in comments.

1 Comment

at the top of php file add header('content-type: application/json; charset=utf-8'); header("access-control-allow-origin: *"); this 2 line is allow to all url for accessing web service
1

You can achieve using Cross-Origin XMLHttpRequest

I.e

$(document).ready(function(){        
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://mersadesign.com/myPlayList.json", true);
    xhr.onreadystatechange = function() {
      if (xhr.readyState == 4) {        
        // JSON.parse does not evaluate the attacker's scripts.
        var resp = JSON.parse(xhr.responseText);
      }
    }
    xhr.send();    
});

It Seems that your server who is returning the json is not supporting the request.

Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.