having problem with jquery ajax

147 views
Skip to first unread message

Aussie Niki

unread,
Jan 19, 2015, 5:37:06 AM1/19/15
to django...@googlegroups.com
my view:

def aget(request):
    
    if request.is_ajax() :
       
        json_data={}
        
        json_data['messege']= 'sorry'
    
        return HttpResponse(json.dumps(json_data), content_type='applicaton/json')
my url:

urlpatterns = patterns('',

    url(r'^$', 'post.views.home', name='home'),
    url(r'^aget/', 'post.views.aget'),
    #url(r'^admin/', include(admin.site.urls)),
)


my templete:

<form onsubmit='return false;'> {% csrf_token %}
<!--    <label for="family_content">Enter a family name to display the NVTs</label>
    <input id="family_content"  /> -->
    <button id='name' onclick='apost()' >ajaxpo</button>
    <button id='name' onclick='aget()' >ajaxge</button>
</form>

<div id='out'>
    
</div>

<script type="text/javascript">

function aget(){
    
  $.ajax({  type: 'GET',
            headers :{"Access-Control-Allow-Origin": "http://127.0.0.1:8000/",
            'Content-Type': 'application/json; charset=utf-8'},
            url : "http://127.0.0.1:8000/aget/",
            dataType:'json',
            async: true,
            data: {
              csrfmiddlewaretoken : '{{csrf_token}}'
            },
          
            success:function(data){
              $('#out').html(data);
              alart(JSON.stringify(data.messege));
            }
  });
   
}
    
</script>

in chrome network:

method: option
status: 200 ok
type :aplication

in chrome console:

XMLHttpRequest cannot load http://127.0.0.1:8000/aget/?csrfmiddlewaretoken=4ES8c0QJnVzLCQH6dqGNmS2q8sxcetoL. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.


James Schneider

unread,
Jan 19, 2015, 7:41:30 AM1/19/15
to django...@googlegroups.com

Quick guess, you are visiting the site in your browser using http://localhost:8000. I'm betting if you instead visited it using http://127.0.0.1:8000, the AJAX query would work fine.

You have http://127.0.0.1:8000 hard coded (or being generated) everywhere. Either change your JavaScript to use localhost, only visit the site using the IP rather than the name, or better yet, try to eliminate any reference to the domain or the IP in your JavaScript by using relative URL's.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/599e8b9b-9164-4dd0-b06a-e98582b7fdd3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages