Ajax请求导致尝试重定向302,而不返回JSON

时间:2018-07-01 00:21:08

标签: ajax asp.net-mvc

我在点击时使用ajax请求来触发MVC中的控制器JsonResult。单击时,收到的是HTML响应而不是JSON,这是我的代码:

$('.platform-list-item').click(function (e) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "@Url.Action("PlatformClick", "To")",
        data: JSON.stringify({ "platform": $(this).find(".platform-click").val() }),
        success: function (data) {
            console.log("worked!");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(jqXHR);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
});

$(this).find(".platform-click").val()是一个字符串值。这是我在ToController.cs中的控制器代码:

[HttpPost]
public JsonResult PlatformClick(string platform)
{
    Debug.WriteLine("PlatformClick");
    Debug.WriteLine(platform);
    return Json("test");
}

这是通过开发人员工具在“网络”标签中显示的两个新事件。我哪里做错了?谢谢!

enter image description here

编辑:每个请求添加HTML视图:

<div class="platform-to-container">
    <div id="platform-container">
        <div class="album-container">
            <div class="album-img-container">
                <img crossorigin="anonymous" id="album-img" src="https://i1.sndcdn.com/artworks-9vhl8uXrclxW-0-t500x500.jpg" />
            </div>
        </div>
        <div class="platform-links-container">
            <div class="artist-song">@Model.Album.AlbumName</div>
            <div class="artist-song-container">
                <img class="artist-img" src="https://i1.sndcdn.com/avatars-000438614124-cca9kq-t500x500.jpg" />
                <div class="artist-name">@Model.Artist.ArtistName</div>
            </div>
            <div class="platform-list-container">
                @if (!String.IsNullOrWhiteSpace(Model.Album.SpotifyLink))
    {
                <div class="platform-list-item">
                    <div class="platform-logo-container">
                        <img class="platform-logo" src="https://s3.amazonaws.com/unlink/platform-images/music-service_spotify_v3.svg" />
                    </div>
                    <div class="platform-btn-container">
                        <div class="link-btn"><i class="fas fa-play"></i>Stream</div>
                    </div>
                    <input type="hidden" class="platform-click" value="spotify" />
                </div>
}
                @if (!String.IsNullOrWhiteSpace(Model.Album.ItunesLink))
    {
                <div class="platform-list-item">
                    <div class="platform-logo-container">
                        <img class="platform-logo" src="https://s3.amazonaws.com/unlink/platform-images/music-service_itunes_v2.svg" />
                    </div>
                    <div class="platform-btn-container">
                        <div class="link-btn"><i class="fas fa-play"></i>Download</div>
                    </div>
                    <input type="hidden" class="platform-click" value="itunes" />
                </div>
}
                @if (!String.IsNullOrWhiteSpace(Model.Album.AppleMusicLink))
    {
                <div class="platform-list-item">
                    <div class="platform-logo-container">
                        <img class="platform-logo" src="https://s3.amazonaws.com/unlink/platform-images/music-service_applemusic_v2.svg" />
                    </div>
                    <div class="platform-btn-container">
                        <div class="link-btn"><i class="fas fa-play"></i>Stream</div>
                    </div>
                    <input type="hidden" class="platform-click" value="appleMusic" />
                </div>
}


            </div>
        </div>
    </div>
</div>

0 个答案:

没有答案