2011-11-23 61 views
1

我的ajax代码没有将值传递给我的webservice方法..我想我没有正确地使用它。请指导我。jquery ajax代码没有将值传递给webservice方法

这是我的.aspx代码:

 $(function() { 
      $.ajax({ 

       type: "POST", 
       url: "WebService.asmx/InsertRediretTime", 
       data: "{ 'ReachTime': '21-Nov-11', 'Destination': 'location' }", 
       contentType: 'application/json; charset=utf-8', 
       dataType: 'json', 
       success: function (data, status) { 
        alert(data.d); 
       } 

      }); 
     }); 

,这是我的web服务方法提前

public static void InsertRediretTime(string ReachTime, string Destination) 
    { 
    //operational code 
    } 

感谢

+0

您是否尝试删除par中的引号ameter名称在JSON中?例如:data:“{ReachTime:'21 -Nov-11',Destination:'location'}”, –

回答

0

试试这个:

$(function() { 
     $.ajax({ 
      type: "POST", 
      url: "WebService.asmx/InsertRediretTime", 
      data: "ReachTime=21-Nov-11&Destination=location", 
      contentType: 'application/json; charset=utf-8', 
      dataType: 'json', 
      success: function (data, status) { 
       alert(data.d); 
      } 
     }); 
    }); 

虽然你服务期待idorder,但你通过ReachTimeDestination - 这是正确的?

+0

对不起,这是错误的......我正在编辑它。但问题是一样的... – nmathur

+0

是的,数据:“ReachTime = 21-Nov-11&Destination = location”,这不是JSON字符串 – manny

0

你得到的错误是什么?请参阅Fiddler(或Firefox上的Firebug)等工具来检查请求/响应 - 请参阅您的ajax请求的响应 - 这将有助于您解决问题。

另外,您需要应用于您的Web服务类的ScriptService属性。如果您使用的是.NET 2.0/3.5,那么您还需要配置条目来注册ScriptHandlerFactory处理程序,该处理程序负责asmx服务中的JSON支持。有关配置的更多信息,请参阅此文章:http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/

+0

没有错误,没有结果.. – nmathur

+0

@nmathur,是否有任何请求在提琴手)?如果是,则必须有来自服务器的HTTP响应(除非请求超时,这是不太可能的)。如果你在本地主机上使用fiddler,那么使用'localhost.'(最后的通知周期),以便在工具中捕获请求/响应。 – VinayC

+0

控制不会去我的web服务(我已经把一个断点)...我不知道什么是问题 – nmathur

3

从您的方法中取出static关键字。

public void InsertRediretTime(string ReachTime, string Destination) 
{ 
//operational code 
} 
+1

希望这会工作 – manny

-1
Try this,  
in aspx page 
    $(function() { 
      $.ajax({ 
       type: "POST", 
       url: "WebService.asmx/InsertRediretTime", 
       data: '{ReachTime:21-Nov-11,Destination:location}', 
       contentType: 'application/json; charset=utf-8', 
       dataType: 'json', 
       success: function (data, status) { 
        alert(data.d); 
       } 
      }); 
     }); 


    In webservice 

    public string InsertRediretTime(string ReachTime, string Destination) 
    { 
    //operational code 
    return stringData; 
    } 
+0

希望这将解决您的问题 –

0

谢谢大家对你的帮助......你的帮助相结合的工作对我..这里是解决方案:

$(function() { 
     $.ajax({ 

      type: "POST", 
      url: "WebService.asmx/InsertRediretTime", 
      data: '{ ReachTime: "21-Nov-11", Destination: "location" }', 
      contentType: 'application/json; charset=utf-8', 
      dataType: JSON, 
      success: function (data, status) { 
       alert(data.d); 
      } 

     }); 
    }); 

,并

public void InsertRediretTime(string ReachTime, string Destination) 
    { 
     blah blah 
    } 

谢谢再次:)