Youtube api 运行 html 没问题,aspx 不正确

Youtube api running fine with html, incorrectly with aspx

我对 aspx 比较陌生,只是试图开始将我的 html 页面与 youtube api 和 javascript 移动到 visual studio 和 aspx 以便移动数据存入数据库。在 html 中,页面似乎工作正常,下一页按钮将成功传递 pageToken。然而,当我将我的 html 代码移动到 aspx 时,我注意到该页面似乎刷新并删除了我的下一个页面标记,重新加载相同的首页。它还给了我一个警告代码,它只出现在 aspx 而不是 html:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.
Setting 'XMLHttpRequest.withCredentials' for synchronous requests is deprecated.

我没有使用我的 jquerys 添加任何脚本,我认为这是出现此警告的主要原因。但是,我的 nextPage 函数确实会调用 api 来满足额外的请求。

在将 html 移动到 aspx 方面,我所做的就是复制所有 html 代码并将其分别放入页眉和正文中。我错过了迁移的一步吗?使用 aspx 而不是 html 时是否必须更改我的代码?

这是我当前的 ASPX 页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="StyleSheet1.css" rel="stylesheet" />
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript" src="JavaScript1.js"></script>
    <script type="text/javascript" src="https://apis.google.com/js/client.js?onload=onClientLoad"></script>
    <style type="text/css">
        #courses {
            width: 566px;
        }
        .auto-style1 {
            width: 556px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div id="video-container">
    <table><tr>
    <td class="auto-style1">
    <h1> Rutgers Accounting Digital Library Directory </h1>
    </td>
    <td align="right"><div id="summary"></div></td>
    </tr>
    <tr><td class="auto-style1"><form action="search_results.asp" method="Post"><input type="text" name="search1" placeholder="Search Here" size="20"><input type="Submit" name="Submit" value="Submit"></form></td><td><form method="link" action="my_uploads.html" ><input type="Submit" name="Clear" value="Clear Filters"></form></td></tr>
    <tr><td colspan="2">
        <form>
        <select id="courses" onchange="show()">
        <option selected disabled>Select a Course</option>
        </select> 
        </form>
    </td></tr>
    <div class="button-container">      
        <tr>
            <td class="auto-style1"><button id="prev-button" class="paging-button" onclick="previousPage();">Previous Page</button></td>
            <td align="right"><button id="next-button" class="paging-button" onclick="nextPage();">Next Page</button></td>          
        </tr>   
        <tr>
            <table id="results"></table>
        </tr>
    </table>        
    </div>

    </div>
    </form>
</body>
</html>

这是我的 javascript 页面:

// Define some variables used to remember state.
var playlistId, nextPageToken, prevPageToken, status, cid;
var totalr = 0;
var rpp = 0;
var sum;
var dur = '';


function onClientLoad() {
    gapi.client.load('youtube', 'v3', handleAPILoaded);
}

// After the API loads, call a function to get the uploads playlist ID.
function handleAPILoaded() {
    gapi.client.setApiKey('APIKEY');
    requestUserUploadsPlaylistId();
}

// Call the Data API to retrieve the playlist ID that uniquely identifies the
// list of videos uploaded to the currently authenticated user's channel.
function requestUserUploadsPlaylistId() {
    // See https://developers.google.com/youtube/v3/docs/channels/list
    var request = gapi.client.youtube.channels.list({
        part: 'contentDetails',
        forUsername: 'rutgersweb'
    });

    request.execute(function (response) {
        cid = response.result.items[0].id;
        dropdown(cid);
        playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads;
        requestVideoPlaylist(playlistId);
    });
}

// Retrieve the list of videos in the specified playlist.
function requestVideoPlaylist(playlistId, pageToken) {
    console.log(pageToken);
    $('#results').html('');
    console.log(pageToken);
    var requestOptions = {
        playlistId: playlistId,
        part: 'snippet',
        maxResults: 50
    };

    if (pageToken) {
        requestOptions.pageToken = pageToken;
    }
    var request = gapi.client.youtube.playlistItems.list(requestOptions);

    request.execute(function (response) {

        totalv = response.pageInfo.totalResults;
        rrp = response.pageInfo.resultsPerPage;
        sum = '<td>Results Per Page: ' + rrp + '</td><td>Total Pages: ' + Math.ceil(totalv / rrp) + '<td>Total Videos: ' + totalv + '</td>';
        $(sum).replaceAll('#summary');

        // Only show pagination buttons if there is a pagination token for the
        // next or previous page of results.
        nextPageToken = response.result.nextPageToken;
        var nextVis = nextPageToken ? 'visible' : 'hidden';
        $('#next-button').css('visibility', nextVis);
        $('#next-button2').css('visibility', nextVis);
        prevPageToken = response.result.prevPageToken
        var prevVis = prevPageToken ? 'visible' : 'hidden';
        $('#prev-button').css('visibility', prevVis);
        $('#next-button2').css('visibility', nextVis);

        var playlistItems = response.result.items;
        if (playlistItems) {
            $.each(playlistItems, function (index, item) {
                displayResult(item.snippet);
            });
        } else {
            $('#results').html('Sorry you have no uploaded videos');
        }

    });
}

function getVideoDetails(mmp, ddp, yyyyp, dur, vidId, videoTitle, used) {
    var request = gapi.client.youtube.videos.list({
        part: 'contentDetails',
        id: vidId
    });
    request.execute(function (response) {
        var str = JSON.stringify(response.result.items[0].contentDetails.duration);
        str = str.replace(/"/g, "");
        str = str.replace(/PT/g, "");
        str = str.replace(/H/g, ":");
        str = str.replace(/M/g, ":");
        str = str.replace(/S/g, "");
        str = str.split(':');
        var ftime;
        if (str[1] < 10) {
            ftime = str[1];
            str[1] = '0' + ftime;
        }
        if (str[2] < 10) {
            ftime = str[2];
            str[2] = '0' + ftime;
        }
        displayOutput(mmp, ddp, yyyyp, str, vidId, videoTitle, used);
    });

}

// Create a listing for a video.
function displayResult(videoSnippet) {
    var videoTitle = videoSnippet.title;
    var videoId = videoSnippet.resourceId.videoId;
    var videoDescription = videoSnippet.description;
    var videoPub = videoSnippet.publishedAt;
    //dur = 

    //PublishedAt Formatting to compare
    ar3 = videoPub.split("T");
    uselessd = ar3[0];
    re = new RegExp("-", "g");
    uselessd2 = uselessd.replace(re, "/");
    usedd = uselessd2.split("/");
    ddp = usedd[2];
    mmp = usedd[1];
    yyyyp = usedd[0];

    //Description trimming to leave time stamps only
    ar = undefined;
    useless = undefined;
    useless2 = undefined;
    used = undefined;
    ar2 = undefined;
    ar = videoDescription.split("Time Stamps:");
    useless = ar[0];
    useless2 = ar[1];

    if (useless2 != undefined) {
        ar2 = useless2.split("Summary");
        ar = ar2[0];
        re = new RegExp("\n", "g");
        useless = ar.replace(re, "<br>");
        used = useless;
    }
    else
        used = " ";
    //  outputArray=[mmp, ddp, yyyyp, dur, videoId, videoTitle,used]
    //      displayOutput(outputArray);

    getVideoDetails(mmp, ddp, yyyyp, dur, videoId, videoTitle, used)
}

function displayOutput(mmp, ddp, yyyyp, dur, videoId, videoTitle, used) {

    //output = '<tr><td colspan="3" align="right">Published On: '+oarray[0]+'/'+oarray[1]+'/'+oarray[2]+'<br>'+dur+'</td></tr><tr><td><img src="http://img.youtube.com/vi/'+oarray[4]+'/1.jpg"></img></td><td><a href="https://www.youtube.com/watch?v='+oarray[4]+'">'+oarray[5]+'</a></td></tr><tr><td colspan="2" align="right">'+oarray[6]+'<hr>';
    if (dur[2])
        output = '<tr><td colspan="3" align="right">Published On: ' + mmp + '/' + ddp + '/' + yyyyp + '<br>Length: ' + dur[0] + ':' + dur[1] + ':' + dur[2] + '</td></tr><tr><td><img src="http://img.youtube.com/vi/' + videoId + '/1.jpg"></img></td><td><a href="https://www.youtube.com/watch?v=' + videoId + '">' + videoTitle + '</a></td></tr><tr><td colspan="2" align="right">' + used + '<hr>';

    else
        output = '<tr><td colspan="3" align="right">Published On: ' + mmp + '/' + ddp + '/' + yyyyp + '<br>Length: ' + dur[0] + ':' + dur[1] + '</td></tr><tr><td><img src="http://img.youtube.com/vi/' + videoId + '/1.jpg"></img></td><td><a href="https://www.youtube.com/watch?v=' + videoId + '">' + videoTitle + '</a></td></tr><tr><td colspan="2" align="right">' + used + '<hr>';

    //Append to results listStyleType
    $('#results').append(output);
}

// Retrieve the next page of videos in the playlist.
function nextPage() {
    console.log(nextPageToken);
    requestVideoPlaylist(playlistId, nextPageToken);
}

// Retrieve the previous page of videos in the playlist.
function previousPage() {
    requestVideoPlaylist(playlistId, prevPageToken);
}


function dropdown(cid) {
    var requestOptions = {
        channelId: cid,
        part: 'snippet',
        maxResults: 50
    };

    var request = gapi.client.youtube.playlists.list(requestOptions);

    request.execute(function (response) {

        var plists = response.result.items;
        if (plists) {
            $.each(plists, function (index, item) {
                var drp = document.getElementById("courses");
                var optn = document.createElement("OPTION");
                optn.text = item.snippet.title;
                optn.value = item.id;
                drp.add(optn);
            });
        } else;

    });
}

function show() {
    var e = document.getElementById('courses');
    var txt = e.options[e.selectedIndex].value;
    requestVideoPlaylist(txt);
}

我知道代码有点乱,仍在努力养成更好的编码习惯,我可能应该修复一些无用的变量。

在您的按钮控件上设置 type="button" 以防止它们在每次按下时都提交页面:

例如,在您的 ASPX 页面中使用:

<td class="auto-style1"><button type="button" id="prev-button" class="paging-button" onclick="previousPage();">Previous Page</button></td>
<td align="right"><button type="button" id="next-button" class="paging-button" onclick="nextPage();">Next Page</button></td>