简体   繁体   English

服务器代码HTTp POST到远程服务器; Javascript API调用

[英]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 jQuery跨域POST恶作剧

I'm programming in Django and javascript 我正在用Django和javascript编程

Steps: 脚步:

  1. ajax post to a local URL - How do I do this? ajax发布到本地URL-我该怎么做? Where do I post this to? 我该在哪里发布?
  2. Server code will do an HTTP POST to remote server - How do I do this in django? 服务器代码将对远程服务器执行HTTP POST-如何在Django中做到这一点?
  3. Send response to JS - I can figure that out. 发送响应给JS-我能弄清楚。

Thanks 谢谢

  1. use the $ajax() function from jquery 使用jquery中的$ ajax()函数
  2. use urllib and urllib2 to access external resources from python. 使用urllib和urllib2从python访问外部资源。 Call these libraries from within your view function 从您的视图函数中调用这些库

Here's an example for the $ajax function: 这是$ ajax函数的示例:

$.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) (这是自动发送短信的api调用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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