2013-07-07 199 views
0

调用C#的webmethod我需要拨打电话到在这个类从阿贾克斯

<%@ WebService Language="C#" Class="emt7anReyady.myService" %> 

using System; 
using System.Web; 
using System.Web.Services; 
using System.Web.Services.Protocols; 
using System.Linq; 
using System.Web.Security; 

namespace emt7anReyady 
{ 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService] 
    public class myService : System.Web.Services.WebService 
    { 
     [WebMethod] 
     [System.Web.Script.Services.ScriptMethod] 
     public void test() 
     { 
      Context.Response.Redirect("http://www.google.com"); 
     } 
    } 
} 

定义一个WebMethod,我必须做出一个Ajax调用这样一个

function getData() { 
       $.ajax({ 
        type: "POST", 
        url: "myService.asmx/test", 
        data: "{}", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function() { 
         alert("succesed") 
        }, 
        failure: function() { 
         alert("faild") 
        } 
       }); 
      } 


调用失败并在Chrome控制台中的问题我得到了这个! Error Image

+0

的你有没有尝试把;在警报声明的末尾。此外,请提及错误,因为在图像中不容易阅读。还有一个建议,在你的ajax调用中使用单引号而不是double。 – rach

+0

这就是问题在于错误是不可读的;没有改变任何东西! –

回答

1

由于使用Response.Redirect,您正在获得ThreadAbort响应。

这是不完全清楚你正在努力实现这个代码是什么 - 如果你想代理你需要阅读和转发响应其他一些网站...

+0

我需要做一些登录,然后转移到本地页面,例如'Thanks.aspx',但我得到了同样的错误 –

0

这是因为Response.Redirect的您正在使用的在你的WebMethod中。如果你真的想重定向到其他页面,请从成功块中执行。

. 
. 

success: function() { 
        window.open('http://www.google.com'); 
       }, 
. 
. 

,你还可以使用document.location代替window.open