简体   繁体   中英

Server Code HTTp POST to remote server; Javascript API call

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

  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)

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