admin管理员组

文章数量:1291643

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

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.

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>

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

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.

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>
Share Improve this question edited May 30, 2012 at 13:01 Henrik Andersson 47.2k16 gold badges100 silver badges94 bronze badges asked May 30, 2012 at 10:36 cheliyancheliyan 1,7873 gold badges16 silver badges22 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 2

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.

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

This may help you :

<html>
<head>
    <script src="http://code.jquery./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>

You didn't include jQuery library...

Add this to the top of <head>:

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

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

The data param you pass to the success function contains your response, and as you're returning JSON it's easy to access any part.

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

本文标签: ajaxHow to call a url from javascript and get response from url to javascriptStack Overflow