Adam Morris
Adam Morris

Reputation: 8545

jQuery.get() not getting response from django http

Odd - I can't seem to use a jQuery.get() to read the response from a django.HttpResponse.

From Django's end, I have a view:

def hello(request):
    return HttpResponse("Hello world", content_type="application/html")

And URL:

urlpatterns = patterns('',
    ('^hello/$', hello),

And when I visit http://localhost:8000/hello/, I see "Hello World" as expected.

In my webpage, though, I do this:

<html><head>
<script src="/asgapp/lib/jquery/jquery-1.4.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
   $(document).ready(function(){
      $('#testdiv').html("initial");
      $.get('http://localhost:8000/hello/', function(data){
         if(data){
            $('#testdiv').html(data);
         }
         else{
            $('#testdiv').html("no data");
         }});
   });
</script>
</head>
<body>
   <h1>test page</h1>   
   <div id="testdiv">(empty)</div>
</body>
</html>

In firebug, I see the request, but the response is empty, even though django has seen the request and processed the response.

Am I missing something? Is this a jQuery issue or a django.HttpResponse thing?

Upvotes: 0

Views: 528

Answers (1)

Nick Craver
Nick Craver

Reputation: 630579

You can't make a request to another domain like this (a different port falls under this), it's blocked by the same origin policy. It's not a jQuery or Django thing, it's how the browser implements security for the XmlHttpRequest object.

To make a request to another domain, you need to use JSONP to pull the data.

Upvotes: 2

Related Questions