2013-07-10 65 views
0

这里歌厅预期的数据是C#Web方法不从jQuery的AJAX调用

[WebMethod] 
    public string Hello() 
    { 
     return "Hello World"; 
    } 

这里是jQuery的Ajax代码但

$.ajax({ 
      url: "http://10.0.2.2/SampleService/Service/HelloWorld.asmx", 
      dataType: 'text', 
      cache: false, 
      crossDomain: true, 
      timeout: 15000, 
      success: function (rtndata) { 
       alert('Got Success ::'+ rtndata); 
      }, 
      error: function (xhr, errorType, exception) { 
       alert("Excep:: "+exception +"Status:: "+xhr.statusText); 
     } 
     }); 

而不是让Hello Worldrtndata我得到充分html响应页面。

回答

0

你需要如果您收到的HTML页面是因为你发送的HTML页面返回一个JSON模式或禁用布局

+0

禁用布局? – iJade

+0

使用这个:http://stackoverflow.com/questions/4164114/posting-json-data-to-asp-net-mvc 看看这家伙是如何使用返回的Json模型。 –

0

您应该仔细检查您的代码,以确定在发送输出之前是否有任何内容附加到响应。

试图发送一个JSON格式的响应,如Ajax调用:

{'hello':'world'} 

然后添加的dataType为$阿贾克斯的选项。如果你想打印输出,你可以做alert(JSON.stringify(response))。

$.ajax({ 
    url: "http://10.0.2.2/SampleService/Service/HelloWorld.asmx", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    cache: false, 
    crossDomain: true, 
    timeout: 15000, 
    success: function (rtndata) { 
     alert('Got Success ::' + JSON.stringify(rtndata)); 
    }, 
    error: function (xhr, errorType, exception) { 
     alert("Excep:: " + exception + "Status:: " + xhr.statusText); 
    } 
}); 

有一件重要的事情......更多时候,你添加缓存:假的,但这并不能正常工作,你应该做的一招解决。

添加时间戳像调用的参数:

var date = new Date(); 
$.ajax({ 
    url: "http://10.0.2.2/SampleService/Service/HelloWorld.asmx?timestamp="+date.now(), 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    cache: false, 
    crossDomain: true, 
    timeout: 15000, 
    success: function (rtndata) { 
     alert('Got Success ::' + JSON.stringify(rtndata)); 
    }, 
    error: function (xhr, errorType, exception) { 
     alert("Excep:: " + exception + "Status:: " + xhr.statusText); 
    } 
}); 

希望这有助于...

0

你有方法名称追加到URL(/HelloWorld,例如),并指定method: "post"在你的ajax调用中(如果你想使用POST请求)。试试这个:

$.ajax({ 
    url: "http://10.0.2.2/SampleService/Service/HelloWorld.asmx/HelloWorld", 
    method:"POST", 
    dataType: "text", 
    cache: false, 
    crossDomain: true, 
    timeout: 15000, 
    success: function (rtndata) { 
     alert('Got Success ::' + rtndata); 
    }, 
    error: function (xhr, errorType, exception) { 
     alert("Excep:: " + exception + "Status:: " + xhr.statusText); 
    } 
}); 

如果你想使用GET作为请求方法,请确保您有这个标签您的web.config文件

<system.web> 
    <webServices> 
    <protocols> 
     <add name="HttpGet" /> 
     <add name="HttpPost" /> 
    </protocols> 
    </webServices> 
</system.web> 

此外,你需要装饰服务方法与ScriptMethodAttribute

[ScriptMethod(UseHttpGet = true)] 
[WebMethod] 
public string HelloWorld() 
{ 
    return "Hello World"; 
} 

AJAX调用(method: "GET"是可选的,因为它是text默认方式):

$.ajax({ 
    url: "http://localhost:57315/helloworld.asmx/HelloWorld", 
    method: "GET" 
    dataType: "text", 
    cache: false, 
    crossDomain: true, 
    timeout: 15000, 
    success: function (rtndata) { 
     alert('Got Success ::' + rtndata); 
    }, 
    error: function (xhr, errorType, exception) { 
     alert("Excep:: " + exception + "Status:: " + xhr.statusText); 
    } 
}); 

当您使用cache: false,大概要use the GET request

Setting cache to false will only work correctly with HEAD and GET requests. 
It works by appending "_={timestamp}" to the GET parameters. 
0

HTML页面,你得到的是所有可用的webmethod在服务商的概述 - 各种API。尝试在浏览器中导航到它。

如果你想打电话给你的WebMethod你好,你应该加上“/你好”作为方法名称的网址:

$.ajax({ 
    type: 'POST', 
    contentType: 'application/json; charset=utf-8', 
    url: 'http://10.0.2.2/SampleService/Service/HelloWorld.asmx/Hello', 
    dataType: 'json', 
    cache: false, 
    crossDomain: true, 
    timeout: 15000, 
    success: function (rtndata) { 
    alert('Got Success ::'+ rtndata); 
    }, 
    error: function (xhr, errorType, exception) { 
    alert("Excep:: "+exception +"Status:: "+xhr.statusText); 
    } 
}); 

你可以在这里阅读更多:http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/