首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >$.ajax GET on _layout.cshtml页面在从Controller/Action页面发布时会导致未定义的错误

$.ajax GET on _layout.cshtml页面在从Controller/Action页面发布时会导致未定义的错误
EN

Stack Overflow用户
提问于 2014-03-07 12:02:54
回答 2查看 685关注 0票数 0

我试图使用$.ajax获得一个变量值,如下所示:

代码语言:javascript
运行
复制
function ConfirmFriend(friendID, addOrDecline) {

      //Ajax Call Get All Employee Records
      $.ajax({
          type: "GET",
          cache: false,
          url: "Home/GetLiveFriends",
          data: {},
          success: function (data) {
              var testnum = data;
              if (testnum == 0 && addOrDecline == "add") {
                  OpenBadges.issue(["https://company.blob.core.windows.net/badges/1.json"]);
              }

              var userID = '@ViewBag.UserID';

              var postData = {
                  'userID': userID,
                  'userFriendID': friendID,
                  'addOrDelete': addOrDecline
              };

              $.post('/User/Show/', postData, function (data) {
              });

              document.getElementById("HiddenPendingFriends" + friendID).style.display = 'none';

              if (addOrDecline == 'add') {
                  document.getElementById("HiddenAcceptedFriends" + friendID).style.display = 'block';
              }
              else {
                  document.getElementById("HiddenDeclinedFriends" + friendID).style.display = 'block';
              }
          },
          error: function (xhr, textStatus, error, exception) {
              alert(error.status + "<--and--> " + error.statusText);
              alert(exception);
              alert(xhr.statusText);
              alert(textStatus.data);
              alert(error);
          }
      });
  };

我的主控制器方法如下所示,并调用HomeViewModel:

代码语言:javascript
运行
复制
public int GetLiveFriends()
{
  return hvm.GetLiveFriends();
}

我的HomeViewModel代码如下:

代码语言:javascript
运行
复制
public int GetLiveFriends()
{
  return friendRepository.Get(f => f.FriendUserID == User.UserID && f.Accepted == 1).Count();
}

如果我从/Admin、/Home、/Profile等调用它,那么它将按预期工作并返回计数,但是如果我从/Admin/page或/Home/page或/Profile/page调用它,那么会产生什么未定义的错误?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-13 09:53:57

我通过将URL更改为url:"/Home/GetLiveFriends“来解决这个问题,在我的评论中已经提到了这一点。

票数 0
EN

Stack Overflow用户

发布于 2014-03-07 12:42:41

试试这个网址:"/Home/GetLiveFriends“。

"Home/GetLiveFriends“是相对的url。目标页取决于当前url地址。"/Home/GetLiveFriends“-绝对在你的领地上。目标地址从域名/*您的绝对网址*开始。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22249591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档