【问题标题】:Open page after XMLHttpRequestXMLHttpRequest 后打开页面
【发布时间】:2016-01-31 07:19:54
【问题描述】:

我在带有参数的页面上发布了 XMLHttpRequest,在本例中为 {file.nodeRef}。现在,我想打开相同的 URL,但使用 post 参数来访问它们。如何打开页面?

我的 XMLHttpRequest 的代码如下:

    var csrf_token = Alfresco.util.CSRFPolicy.getToken();
    var http = new XMLHttpRequest();
    var url = "hdp/ws/my-new-page";

    var params = "file={"+file.nodeRef+"}";
    http.open("POST", url, true);
    //Send the proper header information along with the request
    http.setRequestHeader("Alfresco-CSRFToken", csrf_token);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");

    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    }
    http.send(params);

【问题讨论】:

    标签: javascript http post get xmlhttprequest


    【解决方案1】:

    你为什么不尝试使用 ajaxjquery?

    $.ajax({
        type: "POST",
        url: "hdp/ws/my-new-page",
        beforeSend: function (request)
                {
                    request.setRequestHeader("Alfresco-CSRFToken", csrf_token);
                    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    request.setRequestHeader("Content-length", params.length);
                    request.setRequestHeader("Connection", "close");
                },
        data: "file={"+file.nodeRef+"}",
        dataType: "json",
        success: function(data) {
           window.location.href = data.redirect;       
        }
    });
    

    【讨论】:

    • 为什么要使用ajax jquery?
    • @IvanStefanov 您还有其他解决方案吗?
    • 有人有其他解决方案吗? window.location 从帖子中丢失了参数
    • GET 或 POST 不能解决我的问题...我还有一个问题:stackoverflow.com/questions/33450697/… 但是是的,对于打开的页面,这段代码可以工作。
    猜你喜欢
    • 2021-06-05
    • 2013-04-17
    • 2018-04-23
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    相关资源
    最近更新 更多