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

1

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

A few question: Does the function [def verify1] go in the specific view? for me it's postdate [postdata.html].
My api are originally curl commands, how do I call them? Can I still use urllib2?
I don't understand your first question, and I'm not sure what curl is. Sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.