Unable to retrieve json data using Ajax

Asked 1 years ago, Updated 1 years ago, 80 views

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

<script>
  
$(function(){
    $('button').click(function(){
      $.ajax({
        type: 'GET',
        url: "http://localhost/cakephp-2.6.3_smarty/sample1.json",
        dataType: 'json',
        cache: false,
        success:function(data){

            $('p') .html(data);

        }

      });
    });
});

</script>
<p>Pre-modification</p>
 {"data1":, "data2":"}

I'm running with the above code, but the output doesn't change anything.
If dataType: 'json', is set to dataType: 'html', {"data1":, "data2":"} will be printed.

How can I get the json data?
I would appreciate it if you could let me know.

jquery json ajax

2022-09-29 22:54

1 Answers

{"data1":, "data2":"} is incorrect as JSON. If the contents of sample1.json are {"data1":","data2":"}, it will work.


2022-09-29 22:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.