我简单的jQuery Ajax调用不起作用

时间:2016-10-05 12:27:10

标签: jquery json ajax webservice-client

此网址http://schooltray.com/VsStWsMblApps/SayHello?fullName=Joe%20Smith

获取此信息:

{"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>

2 个答案:

答案 0 :(得分:1)

我已在本地测试了,如果您使用本地JSON,代码正在正确执行。但是如果你想要从那个特定的URL获得Json,我会得到,&#34;访问被拒绝&#34;,

以下是错误,

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请求。

答案 1 :(得分: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>