使用ajax调用webservice,为什么参数没有传进来,可是方法已经找到

xinfeiniao 2015-03-04 11:01:32
JS 脚本如下:
<script type="text/javascript">
function webservice(url, options) {
if (typeof (url) == 'object') { //将url写在options里的情况
options = url;
url = url.url;
}
if (!url) return;
if (options.dataType.toLowerCase() == 'json') { //请求JSON格式的数据时,url后面需要加上“/方法名”
url = url + '/' + options.method;
}
var xmlHttp = getXmlHttp(); //获取XMLHttpRequest对象
xmlHttp.open('POST', url, true); //异步请求数据
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
try {
if (xmlHttp.status == 200 && typeof (options.success) == 'function') {
options.success(xmlHttp.responseText);
}
else if ((xmlHttp.status / 100 == 4 || xmlHttp.status / 100 == 5) && typeof (options.error) == 'function') {
options.error(xmlHttp.responseText, xmlHttp.status);
}
else if (xmlHttp.status / 100 == 200 && typeof (options.complete) == 'function') {
options.complete(xmlHttp.responseText, xmlHttp.status);
}
else if (typeof (options.failed) == 'function') {
options.failed(xmlHttp.responseText, xmlHttp.status);
}
}
catch (e) {
}
}
}
xmlHttp.setRequestHeader('Content-Type', options.contentType); //设置请求头的ContentType
xmlHttp.setRequestHeader('SOAPAction', options.namespace + options.method); //设置SOAPAction
xmlHttp.send(options.data); //发送参数数据
}
function getXmlHttp() {
var xmlHttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlHttp = new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
return xmlHttp;
}
function testPostWebService() {
var data = '<?xml version="1.0" encoding="utf-8"?>'
+ '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
+ '<soap:Body>'
+ '<MsgSendReq>'
+ '<UserName>Krime</UserName><Password>123456</Password><IDCard>123456789145326</IDCard><Moblie>15863254789</Moblie><RealName></RealName><Email>qq@qq.com</Email><VerifyInfo>fsdfsd</VerifyInfo>'
+ '</MsgSendReq>'
+ '</soap:Body>'
+ '</soap:Envelope>';
var options = {
namespace: 'http://tempuri.org/',
method: 'RegUser',
contentType: 'text/xml; charset=utf-8',
dataType: 'xml',
data: data,
success: function (msg) {
alert(msg);
}
};
webservice('http://localhost:7501/RegisterService.asmx', options);
};

</script>
webservice方法如下:
/// 用户注册
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
[WebMethod(Description = "用户注册")]
public string RegUser(string xml)
{
if (string.IsNullOrEmpty(xml))
{
return "<MsgResp><ResultCode>3</ResultCode><ResultMsg>传入数据为空</ResultMsg></MsgResp>";
}
QHSCReg.MsgSendReq reqEntity = getReqFromXml(xml);
return RegUserInfo(reqEntity.UserName, reqEntity.Password, reqEntity.IDCard, reqEntity.Moblie, reqEntity.RealName, reqEntity.Email, reqEntity.VerifyInfo);
}
现在永远只得到传入数据为空的输出,请问为什么,谢谢
...全文
291 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wish907 2015-03-05
  • 打赏
  • 举报
回复
引用 4 楼 xinfeiniao 的回复:
[quote=引用 1 楼 xiaolei8736 的回复:] 换种写法试试~ ajax调用webservice有多种写法
我在网上看到很多是json格式进行访问,可是我这里真的需要xml格式进行处理,能举个例子给我不,谢谢[/quote] 以前用jquery的AJAX里面,想要返回消息为XML还是Json可以自己设置
xinfeiniao 2015-03-05
  • 打赏
  • 举报
回复
没有人回答了吗,鸣鸣
於黾 2015-03-04
  • 打赏
  • 举报
回复
data: data 不用关键字不好吗,你取的变量名跟关键字一样了
  • 打赏
  • 举报
回复
换种写法试试~ ajax调用webservice有多种写法
xinfeiniao 2015-03-04
  • 打赏
  • 举报
回复
引用 1 楼 xiaolei8736 的回复:
换种写法试试~ ajax调用webservice有多种写法
我在网上看到很多是json格式进行访问,可是我这里真的需要xml格式进行处理,能举个例子给我不,谢谢
xinfeiniao 2015-03-04
  • 打赏
  • 举报
回复
引用 2 楼 Z65443344 的回复:
data: data 不用关键字不好吗,你取的变量名跟关键字一样了
这个没有关系吧,即使换个变量也一样

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧