George_Fal 2014-05-11 15:34 采纳率: 0%
浏览 104

.load()传递POST请求

Here is an example loading a django template from an input box using ajax .load().

script for load method

$( document ).ready( function() {
    $( '#searchSubmit' ).click( function() {
        q = $( '#q' ).val();
        $( '#results' ).html( ' ' ).load( '{% url "demo_user_search" %}?q=' + q );
    });
});

from views.py

return render_to_response( template, data, context_instance = RequestContext( request ) )

vs. script for post method

 $.ajax({
        url : "/ajaxexample_json",
        type : "POST",
        dataType: "json",
        data : data2,
        success : function(json) {
            show_results(json); 
        },
        error : function(xhr,errmsg,err) {
            alert(xhr.status + ": " + xhr.responseText);
        }
        });
        return false;
    });

and from the views.py

return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')

I don't know if there is a way for the POST method load a render_to_response template on success, so far I've tried and gotten a 200: error and a pop up (although all of the relevant txt/html is rendered in the pop up).

Is it possible to submit a POST with the .load() function instead?

  • 写回答

1条回答 默认 最新

  • weixin_33730836 2014-05-11 15:48
    关注

    To use POST when using $.load(), you must pass a JSON object as the second parameter. The second parameter (when it is an object) is treated as data and sent to the backend via a POST request.

    Source (Look for 'Request Method')

    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试