简体   繁体   中英

not getting result from ActionResult with $.get

I'm using a jquery get to try and refresh a partialView and its not working. Do I atleast have the syntax right? I'm new to c#

//my javascript is (works fine)

function takeSquare(square) {
   var x = $(square).attr('x');
   var y = $(square).attr('y');

    alert(x + y);

$.get("Home/updateBoardSquare", { posX: y, posY: y }, function (html) {
    $("#gameBoard").replaceWith(html);
});

alert(html);
}

and my c# is

 public ActionResult updateBoardSquare(int posX, int posY){

        String boardHtml = "";

        for (int i = 0; i < 15; i++) {
            for (int k = 0; k < 15; k++) {

                if (board[i, k] == null)
                    board[i, k] = new BoardSquare(i, k);

                if (i == posX && k == posY) 
                    board[posX, posY].takeSquare((String) Session["color"]);

                boardHtml += board[i, k].getHtml();
            }
        }

        ViewData["board"] = boardHtml;

        return PartialView();

    }

I'm just not getting anything at all from the get statement

Internet explorer can't handle relative urls.

Use this:

function takeSquare(square) {
   var x = $(square).attr('x');
   var y = $(square).attr('y');

    alert(x + y);

$.get('@Url.Action("Home", "updateBoardSquare")', { posX: y, posY: y }, function (html) {
    $("#gameBoard").replaceWith(html);
});


}

See? I'm using @Url.Action to let Razor generate the url to use. The upside is that it works both for root web sites and applications which are hosted in virtual directories.

I'm just not getting anything at all from the get statement

You are attempting to use the html variable outside of the success callback.

Also you seem to have inverted use the y variable twice:

$.get("Home/updateBoardSquare", { posX: x, posY: y }, function (html) {
    $("#gameBoard").replaceWith(html);
    alert(html);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM