【问题标题】:jQuery Ajax POST requestjQuery Ajax POST 请求
【发布时间】:2014-05-29 12:50:44
【问题描述】:

我正在尝试获取以下代码以通过 POST 将变量发送到 PHP 页面。我不太确定该怎么做。这是我通过 GET 发送数据并通过 JSON 编码接收数据的代码。我需要更改什么才能通过 POST 将变量传递给 process_parts.php?

function imagething(){
    var done = false,
    offset = 0,
    limit = 20;
    if(!done) {
        var url = "process_parts.php?offset=" + offset + "&limit=" + limit;

        $.ajax({
            //async: false, defaults to async
            url: url
        }).done(function(response) {

            if (response.processed !== limit) {
                // asked to process 20, only processed <=19 - there aren't any more
                done = true;
            }
            offset += response.processed;
            $("#mybox").html("<span class=\"color_blue\">Processed a total of " + offset + " parts.</span>");
            alert(response.table_row);
            console.log(response);
            imagething(); //<--------------------------recursive call
        }).fail(function(jqXHR, textStatus) {

            $("#mybox").html("Error after processing " + offset + " parts. Error: " + textStatus);

            done = true;
        });
    }
}
imagething();

【问题讨论】:

    标签: javascript jquery ajax post request


    【解决方案1】:

    默认方法是GET,要更改它,请使用type 参数。您还可以将查询字符串属性作为对象提供,这样它们在 URL 中就不会立即显现出来:

    var url = "process_parts.php";
    
    $.ajax({
        url: url,
        type: 'POST',
        data: {
            offset: offset,
            limit: limit
        } 
    }).done(function() {
        // rest of your code...
    });
    

    【讨论】:

    • 添加后,如何指定将哪些 post 变量传递给 PHP?
    • 它们会在查询字符串中传递。我已经修改了我的答案,向您展示如何将它们包含在帖子数据中。
    【解决方案2】:

    试试这个

             $.ajax({
                        url: "URL",
                        type: "POST",
                        contentType: "application/json;charset=utf-8",
                        data: JSON.stringify(ty),
                        dataType: "json",
                        success: function (response) {
                            alert(response);
                        },
    
                        error: function (x, e) {
                            alert('Failed');
                            alert(x.responseText);
                            alert(x.status);
                        }
                    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 2016-05-20
      • 2017-11-09
      • 1970-01-01
      相关资源
      最近更新 更多