1

我正在使用 ajax 将 2 个值发布到我的控制器,控制器得到了值但不会显示视图。请协助我在这里做错了什么。谢谢。

Javascript:

function grantPermission() {
        window.FB.login(function (response) {
            if (response.authResponse) {
                uid = response.authResponse.userID;
                accesstoken = response.authResponse.accessToken;
                var postData = { facebookUID: uid, facebookAccessTok: accesstoken };
                $.ajax({
                    url: '@Url.Action("SavePhoto")',
                    data: postData,
                    dataType: 'json',
                    success: function (response) {
                        // process the results from the controller action
                        //  window.location.href = response.Url;
                    }
                });
            } else {
                console.log('User cancelled login or did not fully authorize.');
                alert('User cancelled login');
            }
        }, { scope: 'publish_stream' });
    };

按钮:

   <input type="button" id="auth-loginlink" value="Proceed" onclick="grantPermission();"/>

控制器:

public PartialViewResult SavePhoto(string facebookUID, string facebookAccessTok)
        {
            return PartialView("BlurredPhoto");
        }
4

2 回答 2

1

您需要将返回的 HTML 嵌入到您的文档中的某个位置。例如,如果您添加一个

<div id='MyDiv'>

然后将您的ajax调用更改为

            success: function (responseText, textStatus, XMLHttpRequest) {
                $("#MyDiv").html(responseText);
            }

正如 Slaks 所提到的,您还需要将您的返回数据类型接受更改为 'html'

于 2012-08-24T13:33:18.840 回答
1

您需要在success回调中使用来自服务器的 HTML 源代码。

您可能应该从不告诉 jQuery HTML 是datatype: 'json'.

于 2012-08-24T13:29:12.553 回答