【问题标题】:Unable to send string in HTTP POST无法在 HTTP POST 中发送字符串
【发布时间】:2015-02-11 20:00:31
【问题描述】:

所以,我是 Web 开发的新手,我需要向服务器发送一个 POST 请求。代码如下:

HTML:

<input type="text" class="form-control" placeholder="User ID" name="email" id="email">
<button class="btn btn-theme btn-block" href="#" onClick="httpPOST(email.value)" type="submit">    <iclass="fa fa-lock"></i>SIGN IN</button>

JavaScript:

function httpPOST(data)
{
    var client = new XMLHttpRequest();
    var url = "http://193.136.19.86:8080/restaurants/login/";

    client.open("POST", url, true);
    client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    client.send(data);
}

如果 email.value 中有一个数字,甚至是一个布尔值,则代码可以正常工作。例如,如果我在“email”输入中写“2”,服务器会很好地接收到它。但是,当我写一封真实的电子邮件(或其他字符串)时,它会给我一个 500(内部服务器错误)。

知道我做错了什么吗?

这是使用 Django 开发的服务器视图:

@csrf_exempt
def logtrans(request):
    #print(request)                                                                                                                                     
    context= RequestContext(request,{})
    d=json.loads(request.body)
    if request.method == 'POST':
        print(d)
    return HttpResponse("Done")

提前致谢!

【问题讨论】:

    标签: javascript html django post


    【解决方案1】:

    代码使用“Content-Type”、“application/x-www-form-urlencoded”,因此您的 POST 数据应采用名称:值对的形式,并使用 encodeURI(data) 或 escacpe(data) 进行 urlencoded。

    这个帖子有一般答案Should I URL-encode POST data? 并且还有具体细节的链接。

    【讨论】:

      【解决方案2】:

      除了你的 500 或者它甚至可以修复它:

      你最好使用$.ajax()

      function httpPOST(data){       
         $ajax({
             url: "/restaurants/login/"; // always relative path!
             type: "post", 
             data: {data: data} 
         }).done(function(response){
             if(data.ok == 'ok'){
               alert(data.response);
             }
         });       
      }
      

      aaand your views.py

      from django.core.serializers import json
      
      @csrf_exempt
      def logtrans(request):
         data = {}
         if request.method == 'POST':
             print request.POST.get('data')
             data['ok'] = 'ok'
             data['response']= 'Done'
             return HttpResponse(json.dumps(data), content_type="application/json")
         data['ok'] = 'bad'
         data['response']= 'not post request'
         return HttpResponse(json.dumps(data), content_type="application/json")
      

      顺便说一句,我不会csrf_exempt,小心它。

      【讨论】:

        【解决方案3】:

        我自己从未使用过 Django,但我假设您的问题是由于使用浏览器中的一种编码(URL 编码数据)发送数据,但在您的服务器(JSON)上解码不同。当您尝试发送的数据不是有效的 JSON 字符串时,服务器会抛出异常(这会给您 500 Internal Server Error)。

        因此,解决方案是在任何地方都使用单一编码。例如,要在任何地方都使用 JSON,只需在 JavaScript 代码中更改以下两行:

        client.setRequestHeader("Content-Type", "application/json");
        client.send(JSON.stringify(data));
        

        【讨论】:

        • 非常感谢!简短,简单和正确的答案!就像我喜欢的那样:)
        猜你喜欢
        • 1970-01-01
        • 2017-06-19
        • 1970-01-01
        • 1970-01-01
        • 2015-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-08
        相关资源
        最近更新 更多