Javascript 如何从javascript调用url并从url到javascript获取响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10814576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:03:14  来源:igfitidea点击:

How to call a url from javascript and get response from url to javascript?

javascriptajaxhtml

提问by cheliyan

i want to call url from javascript with one parameter then url has to give the response for that particular request.

我想用一个参数从 javascript 调用 url,然后 url 必须为该特定请求提供响应。

the response is actually like this:

响应实际上是这样的:

{"success":true,
 "result":  {"token":"4fc5ef2bd77a3","serverTime":1338371883,"expireTime":1338372183}
}

if i try this url in browser directly, i can get the same response easily.but through the javascript its not working.

如果我直接在浏览器中尝试这个 url,我可以很容易地得到相同的响应。但是通过 javascript 它不起作用。

I have posted my sample code for testing purpose but there is no response.

我已经发布了用于测试目的的示例代码,但没有响应。

So please help me with how to call and get response?

所以请帮助我如何打电话和得到回应?

Thanks in advance.

提前致谢。

<html>
    <head>
        <script type="text/javascript">
            function getResponse()
            {
                var uname=document.testform.uname.value;

                $.ajax({
                type: 'POST',
                url: 'http://192.168.2.113/crm/webservice.php?operation=getchallenge&username='+uname,
                data: {},
                dataType: 'json',
                success: function(data) 
                { alert('got here with data'); },
                error: function() { alert('something bad happened'); }
                });

            }
        </script>
        <title>Test</title>
    </head>
    <body>
        <form name="testform" method="post">
            <div id="main" border="5" style="width:100%; height:100%;">
                <div id="sub" style="width:50%; height:50%;align:center;">
                    Username:<input type="text" name="uname">
                    <input type="button" name="ok" value="submit" onclick="getResponse();">
                </div>
            </div>
        </form>
    </body>
</html>

回答by Someth Victory

One reason as gdoron said you forgot to include jQuery in your code. But the main thing is you are using ajax with crossing domain name, that the problem. If you type the url in the browser it will work fine because it will request directly from the browser to the site. But you cannot use ajax to send request cross domain like that.

gdoron 说您忘记在代码中包含 jQuery 的原因之一。但最主要的是你使用的是带有交叉域名的ajax,这个问题。如果您在浏览器中输入 url,它将正常工作,因为它将直接从浏览器向站点请求。但是您不能像这样使用ajax发送跨域请求。

I suggest you to see JSONP or ajax cross domain, if you want to get data from different website.

如果您想从不同的网站获取数据,我建议您查看 JSONP 或 ajax 跨域。

回答by Jalaa Zahwa

This may help you :

这可能会帮助您:

<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>

</head>
<body>

<div id="div1" hidden="true"> </div>

<button>Get External Content</button>



<script>
    $("#div1").load('http://192.168.2.113/crm/webservice.php?operation=getchallenge&username='+uname);

    $(document).ready(function(){
        $("button").click(function(){
            var t=$("#div1").text();
            alert(t);

        });
    });

</script>
</body>
</html>

回答by gdoron is supporting Monica

You didn't include jQuery library...

您没有包含 jQuery 库...

Add this to the top of <head>:

将此添加到的顶部<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

If you check your console for errors you will see that $isn't defined.

如果您检查控制台是否有错误,您将看到$未定义的错误。

回答by Dunhamzzz

The dataparam you pass to the successfunction contains your response, and as you're returning JSON it's easy to access any part.

data您传递给success函数的参数包含您的响应,当您返回 JSON 时,可以轻松访问任何部分。

// (inside your ajax call)
success: function(data) {
    alert(data.result.token);
    alert(data.result.serverTime);
    alert(data.result.expireTime);
},