问题描述
什么是我的函数的问题?我想读我的web服务的反应,但我刚刚收到一个错误
浏览器消息是:
当我按下按钮我只是看到我的jQuery的误差函数打电话,但我不知道为什么。请帮我。
函数SetupCompanyInfo(companyID){
//警报(AAA);
companyID ='1';
$阿贾克斯({
型:POST,
网址:'../../../Services/CompanyServices.asmx/GetCompanyInfo',
数据: {},
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON,
成功:的onSuccess,
错误:的OnError
});
}
功能的onSuccess(数据,状态){
SetMainBody(数据);
}
功能的OnError(请求,状态,错误){
SetMainBody(误差+' - '+要求+'状态:'+状态);
}
我的web服务:
使用系统;使用System.Web.Services
;使用System.Web.Script.Services
;
///<总结>
///为CompanyServices
摘要说明///< /总结>
[WebService的空间(namespace =http://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
//[System.ComponentModel.ToolboxItem(false)]
公共类CompanyServices:System.Web.Services.WebService {
[的WebMethod]
公共字符串GetCompanyInfo ()
{
串响应=AAA;
Console.WriteLine(这里);
返回response.ToString();
}
[的WebMethod]
公共字符串GetCompanyInfo(字符串ID)
{
串响应=AAA;
Console.WriteLine(here2+ ID);
返回response.ToString();
}
}
我的aspx文件,头部的一部分和我的按钮代码:
<脚本SRC =../../脚本/ InnerFunctions.jsTYPE =文/ JavaScript的>< / SCRIPT>
<脚本SRC =../../脚本/ jQuery的-1.3.2.min.js类型=文/ JavaScript的>< / SCRIPT>
<脚本SRC =../../脚本/ TabMenu.js类型=文/ JavaScript的>< / SCRIPT>
<脚本SRC =脚本/ InternalFunctions.js类型=文/ JavaScript的>< / SCRIPT>
< DIV DIR =RTL的风格=边界:1px的固体#CCCCCC>
< ASP:图片ID =Image1的=服务器的ImageUrl =../../ generalImg /图标/ 64×64 / settings_Icon_64.gif
风格=宽度:27px;高度:26px的onclick =SetupCompanyInfo(1)/>< / DIV>
的是在正确的道路上 - 你需要做出响应JSON,然后正确地访问在客户端的JSON。您的ASMX方法应该是这样的:
[的WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] / /< ---为了让JSON
公共字符串GetCompanyInfo(字符串ID)
{
Console.WriteLine(here2+身份证);
返回AAA; //不调用的ToString()上的绳子...
}
和您的客户端JS成功函数应该像这样访问数据(您需要访问 D
由ASP.Net生成的属性JSON):
函数的onSuccess(数据,状态){
SetMainBody(data.d); //d是从Web方法的返回
//值的JS对象表示。根据你的情况,它只是
//一个字符串,但它可能是一个更复杂的对象
//或数组
}
当你调用通过ajax的方法,你应该通过在正确的论点也是如此。是这样的:
数据:{ID:'+ companyID +'},
What's my function's problem? I want to read the response from my webservice but I just receive an error.
The browser message is:
when I press the button I just see the error function of my jQuery call but I don't know why. Please help me.
function SetupCompanyInfo(companyID) {
//alert('aaa');
companyID = '1';
$.ajax({
type: "POST",
url: '../../../Services/CompanyServices.asmx/GetCompanyInfo',
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnError
});
}
function OnSuccess(data, status) {
SetMainBody(data);
}
function OnError(request, status, error) {
SetMainBody(error + '- ' + request + ' status:' + status);
}
My webservice:
using System;
using System.Web.Services;
using System.Web.Script.Services;
/// <summary>
/// Summary description for CompanyServices
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
//[System.ComponentModel.ToolboxItem(false)]
public class CompanyServices : System.Web.Services.WebService {
[WebMethod]
public string GetCompanyInfo()
{
string response = "aaa";
Console.WriteLine("here");
return response.ToString();
}
[WebMethod]
public string GetCompanyInfo(string id)
{
string response = "aaa";
Console.WriteLine("here2"+id);
return response.ToString();
}
}
My aspx file,part of head and my button code:
<script src="../../Scripts/InnerFunctions.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="../../Scripts/TabMenu.js" type="text/javascript"></script>
<script src="Scripts/InternalFunctions.js" type="text/javascript"></script>
<div dir="rtl" style="border: 1px solid #CCCCCC">
<asp:Image ID="Image1" runat="server" ImageUrl="../../generalImg/Icons/64X64/settings_Icon_64.gif"
style="width: 27px; height: 26px" onclick="SetupCompanyInfo(1)" /></div>
Cip was on the right path - you need to make the response JSON, and then properly access the JSON on the client side. Your ASMX method should look like this:
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)] // <--- To make it JSON
public string GetCompanyInfo(string id)
{
Console.WriteLine("here2"+id);
return "aaa"; //never call "ToString()" on a string...
}
And your client side JS success function should access the data like this (you need to access the d
property for JSON generated by ASP.Net):
function OnSuccess(data, status) {
SetMainBody(data.d); //"d" is the js object representation of the return
//value from the web method. In your case it's just
//a string, but it could be a more complex object
//or an array
}
When you call the method via ajax, you should pass in the right arguments as well. Something like:
data: "{'id':'" + companyID + "'}",
这篇关于问题阅读Web服务使用jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!