使用JQuery调用ASP.NET PageMethod

时间:2013-09-06 16:01:32

标签: c# javascript jquery asp.net

我正在尝试使用JQuery调用ASP.NET PageMethod,但没有得到任何东西。我当然错过了一些简单但却无法理解的东西,因为我是jQuery的新手。 GetJsonEspaces()不能按预期工作! 请问我能解决这个问题吗?

我的HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script src="js/BiddyScript.js" type="text/javascript"></script>

<div class="boxCanvas">
    <canvas id="myCanvas" width="800" height="600">
      <p>Your browser does not support the canvas element.</p>
    </canvas>
</div>

JavaScript:

var canvas;
var context;

function drawText(text, x, y) {
    context.font = '10pt Helvetica';
    context.fillText(text, x, y);
}

//will be called back!!
function drawBiddies(biddies) {
    var y = 50;
    for (var i = 0; i < biddies.count; i++) {
        var biddy = biddies[i];
        drawText(20, y, biddy.ID + " " + biddy.Libelle);
        y += 20;
    }
}

// A function that takes two parameters, the last one a callback function
function grabBiddies(options, callback) {
    //allUserData.push(options);
    callback(options);
}

//GET DATA FROM CODEBEHIND!!
function GetJsonEspaces() {
    $.ajax({
        type: "POST",
        url: "BiddyCanvas.aspx/GetEspaces",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            grabBiddies (response.d, drawBiddies);
        },
        error: function (data) {

            alert("Could not complete process !");

        }
    });
}

window.onload = function () {
    //grab the context!!
    canvas = $('#myCanvas')[0];
    context = canvas.getContext('2d');

    context.beginPath();                                      //This initiates the border
    context.rect(1, 1, canvas.width - 1, canvas.height - 1);
    context.fillStyle = "#fcfc00";
    context.fill();
    context.lineWidth = 1;                                     //This sets the width of the border
    context.strokeStyle = "#000000";      //This sets the color of the border
    context.stroke();

    //récupérer les données et dessiner!!
    GetJsonEspaces();
}

我的代码隐藏

using System.Web.Services;

        [WebMethod]
        public static string GetEspaces()
        {
            List<BIDDY> oList = GetListEspaces();
            System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string jsonEspaces = oSerializer.Serialize(oList);

            return jsonEspaces;
        }
        ...

谢谢!

0 个答案:

没有答案