用jquery上传文件到asp.net服务获取json响应

时间:2015-09-28 04:17:22

标签: jquery asp.net json ajax web-services

我是这个网络服务的新手。我习惯使用PageMethods。这是我试图做的。我需要使用ajax,天气通过jquery或页面方法,将文件(图片)上传到服务,并返回文本,无xml字符串或json。这是我已经的

使用Javascript:

function uploadFile() {
    var data = new FormData(),
        file = $("#fileToUpload")[0].files[0];

    data.append("name", file.name);
    data.append("size", file.size);
    data.append("type", file.type);
    data.append("file", file);
    data.append("id", $("#hfFlavorId").val);

    $.ajax(
    {
        processData: false,
        //cache: false,
        url: "/upload.asmx/UploadFlavor",
        type: "POST",
        dataType: "json",
        data: data,
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            alert(result);
        },
        error: function (result) {
           alert(result.responseText);
        }


    });
}

WebService的:

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services
Imports System.Web.Script.Serialization
Imports System
Imports System.Collections.Generic

<System.Web.Script.Services.ScriptService()> _
Public Class Upload
    Inherits System.Web.Services.WebService

    <WebMethod(EnableSession:=True)> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function UploadFlavor() As String
        Dim test = HttpContext.Current.Request.Form("name")
        Return "Hihi People - " & test
    End Function
End Class

以下是我的回答:

{"Message":"Invalid JSON primitive: ------WebKitFormBoundaryQznoGE6p0LFrgZsO.","StackTrace":"
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

我做错了什么??? 注意:我想保留这个webservice或pagemethod

0 个答案:

没有答案