2013-11-22 102 views
2

我Post方法使用jquery調用wcf rest服務的POST方法?

[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; 
     } 

    } 

我的Web配置

<?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] 
+0

我碰到過類似的東西......沒有看到測試所有的代碼我不能肯定地說這是問題,但將非字符串數據放入數據爲您的ajax可能會導致該錯誤。由於您的內容類型是JSON,因此請在數據中調用JSON.stringify(im)。 MAYBE這是修復。 – Gobo

+0

非常感謝你@Gobo –

+0

那是答案嗎?如果是這樣,我可以爲你正式回答嗎? – Gobo

回答

0

眼見爲y我們的內容類型是JSON,請在您的AJAX調用的數據區域調用JSON.stringify (im)來解決此問題。由於將非字符串數據作爲AJAX的有效內容傳遞,因此可能會發生此錯誤。