2016-11-07 5 views
0

Я написал простой HTML-страницы, как это:JQuery вебсервис вызов всегда по ошибке

<!DOCTYPE html> 
<HTML> 
<HEAD> 
</HEAD> 
<form name="form"> 
     <input type="text" id="messageTest" name="msg" value="Your message"/> 
     <input type="submit" id="login" /> 
</form> 
<script type="text/javascript" src="jquery.js"></script> 
<script>  
    document.getElementById("login").addEventListener("click", function(e) { 
     e.preventDefault(); 
     $.ajax({ 
      type: "POST", 
      url: "Test.asmx/HelloWorld", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function(msg) { 
       alert(msg.d); 
      }, 
      error: function(xhr,err){ 
       alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status); 
       alert("responseText: "+xhr.responseText); 
      } 
     }); 
    }, false); 
</script> 
</HTML> 

Он всегда возвращается «readyState: 0 Статус: 0 responseText: неопределенные» Все мои файлы находятся в одной папке. Это мой вебсервис:

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
[System.Web.Script.Services.ScriptService] 
public class Test : System.Web.Services.WebService 
{ 
    [WebMethod] 
    public string HelloWorld() 
    { 
     return "Hello World"; 
    } 
} 

Есть ли что-то я делаю не так?

+0

что console.log (err); показывает в консоли? –

+0

В нем отображается «ошибка». – Stan

+0

См. Этот ответ. Возможно, это может быть то, что вам нужно: http://stackoverflow.com/a/11052049/1220550 –

ответ

0

Я добавил следующее перед вызовом АЯКС:

$.support.cors = true; 

Я загрузил свою WebService из VisualStudio и изменил URL для вызова и сейчас работает.

Смежные вопросы