【问题标题】:getting XMLHttpRequest cannot load (URL) Response for preflight is invalid (redirect)获取 XMLHttpRequest 无法加载(URL)预检响应无效(重定向)
【发布时间】:2017-02-04 14:13:24
【问题描述】:

我正在尝试从 sharepoint Online 应用程序的日历列表中获取定期事件,并且正在使用类似的代码

hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
    function GetListData() {
        var webUrl = hostWebUrl;// = "http://server/sitewhereyourlistexists";
        var listGuid = "{2000da75-8663-42d9-9999-ad855c54b4e0}"


        // An XMLHttpRequest object is used to access the web service
        var xhr = new XMLHttpRequest();
        var url = webUrl + "/_vti_bin/Lists.asmx";
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");

        // The message body consists of an XML document 
        // with SOAP elements corresponding to the GetListItems method parameters
        // i.e. listName, query, and queryOptions
        var data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                "<soap:Body>" +
                "<GetListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">" +
                        "<listName>" + listGuid + "</listName>" +
                        "<query>" +
                            "<Query><Where>" +
                                "<DateRangesOverlap>" +
                                    "<FieldRef Name=\"EventDate\"/>" +
                                    "<FieldRef Name=\"EndDate\"/>" +
                                    "<FieldRef Name=\"RecurrenceID\"/>" +
                                    "<Value Type=\"DateTime\"><Today/></Value>" +
                                "</DateRangesOverlap>" +
                            "</Where></Query>" +
                        "</query>" +
                        "<queryOptions>" +
                            "<QueryOptions>" +
                                "<ExpandRecurrence>TRUE</ExpandRecurrence>" +
                            "</QueryOptions>" +
                        "</queryOptions>" +
                "</GetListItems>" +
                "</soap:Body>" +
            "</soap:Envelope>";

        // Here we define what code we want to run upon successfully getting the results
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    var doc = xhr.responseXML;
                    // grab all the "row" elements from the XML results
                    var rows = doc.getElementsByTagName("z:row");
                    var results = "Today's Schedule (" + rows.length + "):\n\n";
                    var events = {};
                    for (var i = 0, len = rows.length; i < len; i++) {
                        var id = rows[i].getAttribute("ows_FSObjType"); // prevent duplicates from appearing in results
                        if (!events[id]) {
                            events[id] = true;
                            var allDay = rows[i].getAttribute("ows_fAllDayEvent"),
                                title = rows[i].getAttribute("ows_Title"),
                                start = rows[i].getAttribute("ows_EventDate");
                            var index = start.indexOf(" ");
                            var date = start.substring(5, index) + "-" + start.substring(2, 4); // get the date in MM-dd-yyyy format
                            start = start.substring(index, index + 6); // get the start time in hh:mm format
                            var end = rows[i].getAttribute("ows_EndDate");
                            index = end.indexOf(" "); end = end.substring(index, index + 6); // get the end time in hh:mm format
                            results += date + " " + (allDay == "1" ? "All Day\t" : start + " to " + end) + " \t " + title + "\n";
                        }
                    }
                    alert(results);
                } else {
                    alert("Error " + xhr.status);
                }
            }
        };

        // Finally, we actually kick off the query
        xhr.send(data);
    }


在 decument 中调用此函数后。就绪部分它没有检索任何数据,但我可以在浏览器控制台中看到如下错误

【问题讨论】:

  • 您好,您是否尝试过安装 Fiddler 然后再次运行 POST? Fiddler 是一个很棒的网络方法调试工具,你可以看到请求和响应。请在您测试后回帖,以便我进一步为您提供帮助。
  • 是的,我已经安装了这个,我该如何调试我的代码?
  • 您只需要打开应用程序(Fiddler)然后在您的应用程序中执行 POST,它就会在网络调用中接听
  • 那之后是什么?意味着下一步是什么?
  • 这正是我得到的——在共享点应用程序中调用 Web 服务(因为重复事件).​​.

标签: javascript jquery sharepoint-apps


【解决方案1】:

您将单击左侧面板中的正确请求,然后在右侧顶部面板中选择“检查器”。然后在不同的请求和响应选项之间进行选择。

【讨论】:

  • 好的,那么它应该告诉我什么,导致上述错误?
  • 几乎是的 -> 否则只需调试您对服务器的调用。请发回您找到的内容,以便我提供进一步帮助。
猜你喜欢
  • 2016-06-06
  • 2016-05-31
  • 2018-02-14
  • 2017-01-19
  • 2018-12-25
  • 2016-03-27
  • 2016-04-10
  • 2017-04-13
  • 1970-01-01
相关资源
最近更新 更多