2013-05-21 2 views
0

WebService에 보이는

using System;<br/> 
using System.Collections.Generic;<br/> 
using System.Linq;<br/> 
using System.Web;<br/> 
using System.Web.Services;<br/> 
[WebService(Namespace = "http://tempuri.org/")]<br/> 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]<br/> 
[System.Web.Script.Services.ScriptService] <br/> 
public class Map : System.Web.Services.WebService 
{ 
    [WebMethod] 
    public MapClass LatitudeLongitude() 
    { 
     MapClass mapobj = new MapClass(); 
     return mapobj; 

    } 
    [WebMethod] 
    public string helloToYou() 
    { 

     return "Hello"; 
} 

} 


처럼 같은 클래스의 모양을

public class MapClass <br/> 
{ 

    string latitude; 

    string longitude; 

    public MapClass() 

    { 

     latitude = "51.508742"; 
     longitude = "-0.120850"; 
    } 

} 

그리고 난이에서 웹 서비스를 호출하는 것을 시도하고있다 html5 페이지

`</head>`<br/> 
`<body>`<br/> 

`<form id="form1" runat="server">`<br/> 

`<h1>Map</h1>`<br/> 

`<input id="View" type="button" value="Click to view" onclick="getData()"/>`<br/> 

`</form>`<br/> 

`</body>`<br/> 


`<script type="text/javascript" src="D:\PepsiCO\jQuery\jquery-1.7.1.min.js"> </script>`<br/> 

`<script type="text/javascript">` 

    function getData() { 

     $.ajax({ 

      type:"POST", 
      url: "http://localhost:53788/HTML5_WebService_Maps/Map.asmx/helloToYou", 
      contentType: "application/json;charset=utf-8", 
      dataType: "json", 
      complete: function (response) 
      { alert('complete'); }, 
      success: function (response) 
      { alert('Success'); }, 
      error: function (response) 
      { alert('Failure'); } 
     }); 
    } 
`</script>` 
`</html>` 

크롬에서 html 페이지를 탐색하면 경고 상자에 오류가 표시됩니다. 성공하지 못하는 이유는 모르겠다. 도와주세요.

+0

'data :'{} ','를 ajax 함수에 추가하십시오. 비록 그것이 효과가 있을지 확실하지 않습니다. – thunderbird

+1

서버가 콘텐츠 유형'application/json'을 반환하는지 확인하십시오. – muneebShabbir

+0

또한 URL을'url : "Map.asmx/helloToYou", ' – thunderbird

답변

0

의 WebMethod이를 추가하십시오 :

[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 

그리고 시리얼 라이저와 개체를 반환 :

error: ajaxFailed 

function ajaxFailed(xmlRequest) { 
$('#errordiv).html(xmlRequest.status + ' <br /> ' + 
      xmlRequest.statusText + '<br />' + 
      xmlRequest.responseText); 
} 
:

JavaScriptSerializer js = new JavaScriptSerializer(); 
string strJSON = js.Serialize(mapobj); 
return strJSON; 

는 또한 오류 응답을 게시하는 것이 좋습니다 수 있습니다

이 답변에 대한 답변에 : 인용 https://stackoverflow.com/users/158502/langdon '내 경험에 의하면, 사이트 간 스크립팅 (액세스가 거부 된 경우) 또는 도달 할 수없는 URL (오타, DNS 문제 등)을 요청할 때 상태 0이 표시됩니다.'

jQuery Ajax - Status Code 0?

+1

반환해야하는 데이터를 직렬화 할 필요가 없습니다. asp.net 자동으로 않습니다. – thunderbird

+0

상태는 ** 0 **입니다. statusText는 ** error **입니다. responsetext is blank ** – Libin

+0

내 경험에 의하면, 사이트 간 스크립팅 (액세스가 거부 된 경우) 또는 도달 할 수없는 URL (오타, DNS 문제 등)을 요청할 때 상태 0이 표시됩니다. from : http://stackoverflow.com/questions/2000609/jquery-ajax-status-code-0 – Yeronimo

0

변화 데이터 유형은 웹 페이지 실행이 같은 도메인/포트

dataType: "html", 
0

가 확인 htmland하는? 응답의 상태 코드는 무엇입니까?

+0

상태 코드를 제공합니다 : error/0 – Libin

+0

도메인/포트는 어떨까요? 크롬 콘솔의 모든 항목? – mguimard

+0

코드가 서버 – Libin