Tag Archives: JSON

JSON Issue: This request has been blocked because sensitive information – MVC

Hey everyone,

Started redoing Learner Lessons in MVC4 and AngularJS today. I ran into this little issue while trying to retrieve JSON:

This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet

It turns out that this behavious is intentionally prevented in order to avoid JSON hijacking. Thankfully the data I’m working with is not at all sensitive so the workaround is pretty straight forward:

public ActionResult Index()
{
     return Json(db.Regions.ToList(), JsonRequestBehavior.AllowGet);
}

Source: http://stackoverflow.com/a/4616442/522859

For those of you working with sensitive data that don’t want to risk exposing it, posting is suggested as an alternative on StackOverflow: http://stackoverflow.com/a/6440163/522859

Missing Partial (JSON Response) – Ruby on Rails

Hey everyone,

I’ve just added pagination (Kaminari) to make an infinite scroll list a little easier to use. Unfortunately, I ran into the following error – had me stumped for a while:

Missing partial /contents/content_list_item with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee]}. Searched in:  * "/home/chris/funny/app/views"  * "/home/chris/.rvm/gems/ruby-1.9.3-p327/gems/kaminari-0.14.1/app/views"

The solution was fairly simple, but adding a few things at once had me looking at it the wrong way. Because I was using the same nested partials for an HTML response as my JSON one, the file extensions weren’t what rails was expecting. To get around this, all I needed to do was fully qualify the partial filenames. For instance:

1
2
3
4
5
#The original
<%= render :partial => '/contents/content_list_item', :locals => { :@content_item =>  content} %>
 
#Became this
<%= render :partial => '/contents/content_list_item.html.erb', :locals => { :@content_item => content} %>

Remote JSON Source – Ruby on Rails

Hey everyone,

Just a quick post on how to retrieve a remote JSON source so I don’t forget.

1
2
3
4
5
6
7
8
9
10
11
require 'open-uri'
require 'json'
 
#Retrieve source (controller/model)
@results = JSON.parse(open("http://www.myjsonsource.com/jsonfeed_num1").read)
 
 
#In view
<% @results.each do |result| %>
   <%= result['value'] %>
<% end %>

That’s all there is to it, let me know if you have any problems!

Events Only Showing in Month View – FullCalendar

Hey everyone,

Ran into a bit of a problem with FullCalendar today after setting up a JSON feed. My events were only appearing in the month view. This StackOverflow post explains that this is due to the fact that the allDay property wasn’t set.

Original JSON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[
    "0",
    {
        "title": "Test event",
        "id": "821",
        "start": "2012-11-23 14:00:00",
        "end": "2012-11-23 15:00:00"        
    },
    "1",
    {
        "title": "Test event 2",
        "id": "822",        
        "start": "2012-11-23 9:00:00",
        "end": "2012-11-23 10:00:00"
    },
    "2",
    {
        "title": "Test event 3",
        "id": "823",        
        "start": "2012-11-24 8:00:00",
        "end": "2012-11-24 6:00:00"
    },
    "3",
    {
        "title": "Test event 4",
        "id": "824",        
        "start": "2012-11-27 6:00:00",
        "end": "2012-11-27 7:00:00"
    }
]

Working JSON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[
    "0",
    {
        "allDay": "",
        "title": "Test event",
        "id": "821",
        "start": "2012-11-23 14:00:00",
        "end": "2012-11-23 15:00:00"        
    },
    "1",
    {
        "allDay": "",
        "title": "Test event 2",
        "id": "822",        
        "start": "2012-11-23 9:00:00",
        "end": "2012-11-23 10:00:00"
    },
    "2",
    {
        "allDay": "",
        "title": "Test event 3",
        "id": "823",        
        "start": "2012-11-24 8:00:00",
        "end": "2012-11-24 6:00:00"
    },
    "3",
    {
        "allDay": "",
        "title": "Test event 4",
        "id": "824",        
        "start": "2012-11-27 6:00:00",
        "end": "2012-11-27 7:00:00"
    }
]

Format JSON – JSONLint

Hey everyone,

Just a site that I came across today that was pretty useful for JSON: http://jsonlint.com/

I’ve been working with jQuery FullCalendar and needed some sample data. It has the option to use a JSON feed but mine wasn’t working for some reason. JSONLint allowed me to both format and validate it.

Let me know if you come across any other useful formatters/validators.

Cheers,
Chris