最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get a response from a javascript post request - Stack Overflow

programmeradmin1浏览0评论

So I'm using this post request I found on here, but I'm wondering how I receive a response using this...

function post_to_url(path, params, method) {
    method = method || "post"; 
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
         }
    }ody.appendChild(form);
    form.submit();
}

Thanks in advance.

So I'm using this post request I found on here, but I'm wondering how I receive a response using this...

function post_to_url(path, params, method) {
    method = method || "post"; 
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
         }
    }ody.appendChild(form);
    form.submit();
}

Thanks in advance.

Share Improve this question asked Feb 13, 2013 at 16:24 Ostap HnatyukOstap Hnatyuk 1,2762 gold badges15 silver badges21 bronze badges 1
  • have a look at AJAX. It's better for your purpose than submitting a form. – Sirko Commented Feb 13, 2013 at 16:26
Add a ment  | 

1 Answer 1

Reset to default 1

This code configures a form and submits it. You don't get any response to javascript, because the page reloads.

Try using jQuery: http://api.jquery./jQuery.post/

Or plain javascript AJAX: https://developer.mozilla/en/docs/AJAX if you want to write the whole thing yourself

发布评论

评论列表(0)

  1. 暂无评论