簡體   English   中英

ASP.NET AJAX查詢返回頁面

[英]Asp.net ajax query returning page

我首先嘗試在頁面中使用jQuery運行ajax請求。 在查看頁面上localhost:1382/Home/Index我有以下代碼:

...bla-bla, another usless code..
 $.ajax({
                        url: "Index/ShowStadium",
                        contentType: "application/json; charset=utf-8",
                        dataType: "text",
                        type: "POST",
                        data: "",
                        success: function (data) {
                            $(".right-content").html(data);
                        },
                        error: function (xhr, textStatus) {
                            alert(textStatus);
                            $(".right-content").html("в этом клубе нет спортивных площадок");
                        }

                    });
...other usless code...

所以我在HomeController放置了一個名為ShowStadium()的函數。 但是每次我得到以下錯誤之一:

  1. 200-解析錯誤。
  2. 好的,但是響應是一整頁。

這是我的HomeController代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectMVC.Models;
using System.Web.Services;

    namespace ProjectMVC.Controllers
    {
        public class HomeController : Controller
        {
            private readonly Initializer init = new Initializer();

            public ActionResult Index()
            {
                return View();
            }

            [HttpPost]
            public ActionResult Index(EventDetails obj)
            {
                if (ModelState.IsValid)
                {
                   ViewData["Error"] = "nice one!";
                   return Redirect("/home/index2.cshtml");
                }
                else
                {
                    ViewData["Error"] = "Заполните необходимые поля, пожалуйста";  // If JS disabled
                }
                return View();
            }

            [WebMethod]
            public static string ShowStadium()
            {
                return "Футбольное поле";
            }
        }
    }

正如我所說,對我來說最大的問題是響應頁面,即返回HomeController的視圖頁面。

您不需要webMethod來執行此use操作:

public ActionResult ShowStadium()
{
    return Content("Футбольное поле");
}

javascript:

$.ajax({
      url: "HOme/ShowStadium",
      contentType: "application/json; charset=utf-8",
      dataType: "text",
      type: "POST",                       
      success: function (data) {
          $(".right-content").html(data);
      },
     error: function (xhr, textStatus) {
          alert(textStatus);
          $(".right-content").html("в этом клубе нет спортивных площадок");
     }
  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM