2014-11-06 135 views
0

我想調用一個web服務使用ajax,即時獲取內部500錯誤。可以請指教即時消息不知道什麼即時做錯了,我可以調用一個webmethod沒有問題。與jquery ajax jsonp調用webservice:錯誤

JQUERY AJAX CALL 

<script type="text/javascript"> 

function LoginVailid() { 

    $.ajax({ 
     url: "http://localhost:49301/AppService.asmx/LoggonAuthentication", 
     contentType: "application/json; charset=utf-8", 
     data: "{}", 
     dataType: "jsonp", 
     jsonp: "callback", 
     crossDomain: true, 
     success: function (json) { 
      alert(json.d); 
     }, 
     error: function() { 
      alert("Hit error fn!"); 
     } 
    }); 
} 
</script> 

WEBSERVICE方法

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 

[System.Web.Script.Services.ScriptService] 
public class Service1 : System.Web.Services.WebService 
{ 

    [WebMethod] 
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
    public string LoggonAuthentication() 
    { 
     return "Hello World"; 
    } 

} 
+0

你能從'jsonp'嘗試改變'dataType'到'JSON ' – 2014-11-06 16:10:05

+0

如果我這樣做然後獲得以下ERROR.XMLHttpRequest不能廣告http:// localhost:49301/AppService.asmx/LoggonAuthentication?{}。請求的資源上沒有「Access-Control-Allow-Origin」標題。原因'http:// localhost:55255'因此不被允許訪問。該響應具有HTTP狀態代碼500. – 2014-11-06 16:11:34

+0

>我正在嘗試使用ajax調用Web服務,即時獲取內部500錯誤。 你能調試和檢查真正的異常嗎? – 2014-11-06 16:17:31

回答

0

試試這個代碼

[WebMethod] 
[ScriptMethod(UseHttpGet = true, XmlSerializeString=false, ResponseFormat = ResponseFormat.Json)] 
public string LoggonAuthentication(string callback) 
{ 
    return callback + "({message: 'Hello World'})"; 
} 

而且這個articlethis SO question可以幫助