使用jquery调用wcf rest服务的POST方法?

时间:2013-11-22 15:15:50

标签: jquery wcf

我的帖子方法

[OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "Service/UpLoadCarPhoto",
        BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    public string UpLoadCarPhoto(Stream imageFile)
    {

        try
        {
            _db.CarPhotoes.Add(new CarPhoto
            {
                CarPhotoId = 7,
                CarId = 2,
                CarPhoto1 = ReadFully(imageFile)
            });
            _db.SaveChanges();
            return "Photo Saved";
        }
        catch (Exception ex)
        {

            return ex.InnerException.InnerException.Message;
        }

    }

我的网站配置

<?xml version="1.0"?>
<configuration>

  <system.web>
      <compilation debug="true" targetFramework="4.0">
        <assemblies>
          <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </assemblies>
      </compilation>
  </system.web>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <connectionStrings>

    <add name="CarHaatDbContext" connectionString="metadata=res://*/Entities.CarHaatModel.csdl|res://*/Entities.CarHaatModel.ssdl|res://*/Entities.CarHaatModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CarHaatDatabase.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=False&quot;" providerName="System.Data.EntityClient" />
    <!--<add name="CarHaatDbContext" connectionString="Data Source=.\SQLEXPRESS; 
AttachDbFilename=F:\8-Semester Project\11-20-13\CarHaat\CarHaatWcfService\App_Data\CarHaatDatabase.mdf;
Integrated Security=True; Connect Timeout=30; User Instance=True" />-->
  </connectionStrings>


</configuration>

我如何调用此服务

@{
    ViewBag.Title = "Home Page";
}
<script src="@Url.Content("~/Scripts/jquery-1.10.2.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.base64.js")" type="text/javascript"></script>


<input type="file" name="UploadImage" id="CarImage"/>
<input type="button" id="uploadImage"/>



<script type="text/javascript">

    var getCarUrl = 'http://localhost:62051/Service/GetCarList';
    var postCarUrl = 'http://localhost:62051/Service/UpLoadCarPhoto';

    $('#uploadImage').click(
    function () {
        var im = $('#CarImage').get(0).files[0];
        alert(im);
        $.ajax({
            cache: false,
            type: "POST",

            url: postCarUrl,

            data: im,
            contentType: "application/json; charset=utf-8",
            success: function (response) {
                alert(response);
            },
            error: function (xhr) {
                alert(xhr.status);
            }
        });

            }
    );


</script>

我收到以下错误

NS_NOINTERFACE: Component does not have requested interface [nsIDOMBlob.slice]

1 个答案:

答案 0 :(得分:0)

看到您的内容类型是JSON,在AJAX调用的数据区域中调用JSON.stringify ( im )应该可以解决此问题。由于非字符串数据作为AJAX的有效负载传递,因此可能发生此错误。