2016-03-09 22 views
1

我已经创建了一个WCF宁静服务。如何在wcf服务中获取Xml作为字符串并使用javascript发送?

public string Demo(String xmlString) 
    { 
     //do stuff 
    } 

    [OperationContract] 
    [WebInvoke(ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, Method = "Post", UriTemplate = "Demo", 
    BodyStyle = WebMessageBodyStyle.Wrapped)] 

string Demo(String xmlString); 

我通过

$(document).ready(function() { 
    $("#btn").click(function() { 
    var bhRequest = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" + 
      "<s:Body>" + 
      "<GetSMC xmlns=\"http://tempuri.org/\">" + 
       "<value><Root>MyValue</Root></value>" + 
      "</GetSMC>" + 
      "</s:Body>" + 
     "</s:Envelope>";   
    var bhReq="<![CDATA[" + bhRequest + "]]>"; 
    alert(bhReq); 

     $.ajax({ 
      url: 'http://localhost:15536/Plugins/MyPlugin/RemoteService/WebService.svc/Demo', 
      type: 'POST', 
      data: '{"xmlString":"'+ bhReq +'"}', 
      contentType: "text/xml", 
      dataType: "xml", 
      success: function (data) { 
       alert(Successfull); 
      }, 
      error: function (data) { 
       alert('Error Occurred'); 
      } 
     }); 
    }); 
}); 

呼叫发信不会服务,并给出了

NetworkError: 405 Method Not Allowed

XML Parsing Error: not well-formed Location: moz-nullprincipal:{70ef8883-a52b-4e70-a1ca-bdf5c611c23c} Line Number 1, Column 1:

{"xmlString":"MyValue</Root></value></GetSMC></s:Body></s:Envelope>]]>"}

的错误,我也通过一些文字,是通过我的服务,但XML字符串没有通过。

我也已经完成了使用将我的服务请求和响应格式设置为json,并使用数据类型JSON从我的脚本传递数据,它也不起作用。

请给任何解决方案如何我可以将xml作为字符串值从javascript传递给我的wcf rest服务。

+0

@Mozts给了你正确的答案。将代码:data:'{“xmlString”:“'+ bhReq +'”}'更改为data:xmlString。并添加一个[xml头文件](http://stackoverflow.com/questions/7007427/does-a-valid-xml-file-require-an-xml-declaration),如<?xml version =“1.0”?>和删除CDATA。标签。 – Roberto

回答

1

方式的用户,下面有两种选项之一,改变WCF的消息JSON或设置的XML肥皂格式正确

对于方案一读 http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

对于方案二 你可以阅读https://leonidius2010.wordpress.com/2011/05/16/98/

对于这个选项,我首先看到你的示例xml的一些问题,你正在包装完整的xml格式不正确的cdata。

我还看到xml文档类型在您要发送的有效内容中缺失。

为了获得正确的JavaScript格式,我会在Visual Studio中使用wcf测试客户端来调用您的端点,然后复制并传递正在发送到JavaScript文件的有效内容。

+0

已经投票确定了多个问题并提出了两个解决方案。 – Roberto

相关问题