0

I found an answer to a question that tells what to do, but I don't know how to implement it.

jQuery cross domain POST shenanigans

I'm programming in Django and javascript

Steps:

  1. ajax post to a local URL - How do I do this? Where do I post this to?
  2. Server code will do an HTTP POST to remote server - How do I do this in django?
  3. Send response to JS - I can figure that out.

Thanks

Community
  • 1
  • 1
Marcus
  • 9,032
  • 11
  • 45
  • 84
  • 1) You create your own URL that runs your server-side code. 2) https://www.google.com/search?q=dhango+http+post – SLaks Aug 06 '13 at 23:23

1 Answers1

1
  1. use the $ajax() function from jquery
  2. use urllib and urllib2 to access external resources from python. Call these libraries from within your view function

Here's an example for the $ajax function:

$.ajax({
    type: "GET",
    url: '/htmlApi/sendSms/',
    data: {
        'phone':'+12412354135',
        },
    success: function(data){
        $("#ajaxDestination").html(data);
    }
});         

here's an example of a view function that posts data to the remote server:

def verify1(request):
    u = request.session['user']
    u.phone_number = request.GET['phone']
    u.save()


    apiUrl = "http://www.XXXXXXXXX.net/api/send.aspx?username=XXXXXXX&password=XXXXXX&language=1&sender=XXXXXX&mobile=" + request.GET['phone'] + "&message=" + 'ghis' + " is your verification code."
    x = urllib2.urlopen(apiUrl).read()


    return HttpResponse(x)

(This is an automated sms sending api call)

Bit68
  • 1,446
  • 10
  • 25