2016-10-05 21 views
0

此URL http://schooltray.com/VsStWsMblApps/SayHello?fullName=Joe%20Smith我简单的jQuery Ajax调用不起作用

获取此:

{"SayHelloResult":"{\"Status\":1,\"Message\":\"Hello Joe Smith\"}"}

但我JQuery不能正常工作。完整的HTML页面如下所示。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Say Hello</title> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(function() { 
      $('#btn_SayHello').click(function (e) { 
       $.ajax({ 
        type: 'Get', 
        url: 'http://schooltray.com/VsStWsMblApps/SayHello?fullName=Joe%20Smith', 
        dataType: 'json', 
        success: function (response) { 
         alert('Success'); 
        }, 
        error: function (jqXHR, textStatus, errorThrown) { 
         alert('Error = ' + errorThrown + ' -- ' + jqXHR.responseText); 
        } 
       }); 
       e.preventDefault(); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    Full Name:<br /> 
    <input id="txt_Email" type="text" style="width: 300px;" /> 
    <br /><br /> 
    <input id="btn_SayHello" type="button" value="Say Hello" /> 
    <br /><br /> 
</body> 
</html> 
+1

你得到任何错误? –

+0

它以什么方式不起作用? – andrew

+0

它总是陷入错误部分。并且所有错误字符串都是空的。 – GAksut

回答

1

那么我已经在我的本地测试,如果您使用本地JSON代码正确执行。但是,如果从perticular URL想要的JSON,我收到“拒绝访问”,

下面是错误,

XMLHttpRequest cannot load http://schooltray.com/VsStWsMblApps/SayHello?fullName=Joe%20Smith. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://cons.you.com' is therefore not allowed access.

即你正在做一个XMLHttpRequest到不同的域,你没有访问控制。因此,浏览器阻止http请求。

0

您可以创建一个表单,并将该表单内所有的投入和尝试

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Say Hello</title> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(function() { 
      $('#btn_SayHello').click(function (e) { 
       $.ajax({ 
        type: 'Get', 
        url: 'http://schooltray.com/VsStWsMblApps/SayHello?fullName=Joe%20Smith', 
        dataType: 'json', 
        success: function (response) { 
         alert('Success'); 
        }, 
        error: function (jqXHR, textStatus, errorThrown) { 
         alert('Error = ' + errorThrown + ' -- ' + jqXHR.responseText); 
        } 
       }); 
       e.preventDefault(); 
      }); 
     }); 
    </script> 
</head> 
<body> 
<form name="formname"> 
    Full Name:<br /> 
    <input id="txt_Email" type="text" style="width: 300px;" /> 
    <br /><br /> 
    <input id="btn_SayHello" type="button" value="Say Hello" /> 
    <br /><br /> 
</form> 
</body> 
</html>