Ajax.Request 调用 WebService问题!极为困惑!

蝈蝈太阳 2008-04-15 01:57:33
the code as the following:
------------------------------------------------------------------------------------------
var url = 'http://localhost/Test3/WebService.asmx/HelloWorld';
var params = {mothod: 'post',
contentType: 'application/json',
encoding: 'utf-8',
/*postBody: {haha:'\''+$F(this.input)+'\''},*/
parameters: {haha:$F(this.input)},/**/
onSuccess: this.onajaxSuccess.bind(this),
onFailure: this.onajaxFailure.bind(this),
onException: function (e) {
otherValues+='exception<br>'+e.url+'<br>'+inspect(e)
+'<br>'+e.getHeader()+'<br>'+e.getStatus()+'<br>';
otherValues+=inspect(e.transport)+'<br>';
blackboard.update(otherValues);
},
onComplete: function (transport) {otherValues
+='completed<br>'+inspect(transport); blackboard.update(otherValues);}
};
this.request = new Ajax.Request(url, params);
-------------------------------------------------------------------------------------------
The web service is defined as the following:
-------------------------------------------------------------------------------------------
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Web.Script.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld(string haha)
{
string temp = "[{'key':'1', 'value':';lakjshf 787agf8 ..1671
111'},{'key':'2', 'value':'[lakjshf 787agf8 ..1671 111'},{'key':'3',
'value':'[lakjshf 787agf8 ..1671 333'}]";

return temp;
}
}

---------------------------------------------------------------------------------------------

if no parameters are transfered to the method, it is ok.
once passing some parameters in, the following error shows:
------------------------------------------------------------------------------------------------------------------------
System.InvalidOperationException: Request format is invalid:
application/json; charset=utf-8.
System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
============================================================
...全文
271 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
live_7sky 2008-04-24
  • 打赏
  • 举报
回复
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld(string haha)
{
string temp = "[{'key':'1', 'value':';lakjshf 787agf8 ..1671
111'},{'key':'2', 'value':'[lakjshf 787agf8 ..1671 111'},{'key':'3',
'value':'[lakjshf 787agf8 ..1671 333'}]";

return temp;
}

返回的值 被 [WebMethod] 序列化了,并不是 JSON .

为什么想到用 AJAX 调用 WEBSERVICES(返回XML),而且 返回 JSON.
蝈蝈太阳 2008-04-15
  • 打赏
  • 举报
回复
{"Message":"Invalid JSON primitive: .","StackTrace":"
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)
System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)
System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

52,797

社区成员

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

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