简体   繁体   中英

how to get the back data from a django view using ajax

my ajax is :

$('#save').click(function(){
            $.post("http://127.0.0.1:8080/sss",
           function(data){
             alert(data);
           });
            })

and the django view is :

def sss(request):
    return HttpResponse('ddddddddddd')

how to get some data from the view 'sss'

thanks

You are running into a cross domain issues . You cannot perform ajax calls to different domains. You could use JSONP instead (look at the Flickr example in the documentation, it demonstrates a cross domain ajax request). Your server needs to send data as JSONP string:

def sss(request):
    return HttpResponse('someCallbackName({ Data = 12345 })')

where someCallbackName should be dynamic and passed as request parameter. An important note is that JSONP works only with GET methods and not POST .

a hack to do cross domain scripting is to read the data in using urlopen and returning the data you receive

while on domain1

import urlllib2

def getdata(req)
    redirectstr = "http://domain2.com/call/that/returns/data/"
    #make call to domain2
    resp = urllib2.urlopen(redirectstr)
    return HttpResponse( resp.whatever() )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM