[Answer]-Server Code HTTp POST to remote server; Javascript API call

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

Leave a comment