0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - I didn't realize a port counted as a different domain!

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.