如何将字符串值从javascript传递到ASHX中的函数

时间:2013-02-20 20:37:58

标签: javascript ajax parameters ashx

我使用此代码将参数从jQuery传递到ASHX,实际上我想使用Uploadify插件上传文件并将名为“Id”的参数发送到ASHX

function CallHandler() {
    $.ajax({
        url: "PIU.ashx/MyMethod",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: { 'Id': '10000' },
        responseType: "json",
        success: OnComplete,
        error: OnFail
    });
    return false;
}

function OnComplete(result) {
    alert(result);
}
function OnFail(result) {
    alert('Request Failed');
}

和ASHX代码:

public void ProcessRequest(HttpContext context)
{
    var employee = Convert.ToInt32(context.Request["Id"]);

    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
    string serEmployee = javaScriptSerializer.Serialize(employee);
    context.Response.ContentType = "text/html/plain";
    context.Response.Write(serEmployee);

    parent MyParent = (parent)context.Session["mahdZNUparent"];

    //the file data is the file that posted by Uploadify plugin
    HttpPostedFile PostedFile = context.Request.Files["Filedata"];

    string FileName = PostedFile.FileName; // whene i comment this line, the code works
    // properly, but when uncomment this line, the code get to 'Request Failed'
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

我怎样才能解决这个问题!!!!!!!

1 个答案:

答案 0 :(得分:1)