duanlaofu4108 2015-02-26 06:13
浏览 33
已采纳

我正在尝试实现一个AJAX调用。 我究竟做错了什么?

This is my HTML text:

<input type="text" class="resizedsearch" name="searchdb">
<button id="submit" onclick="ajaxCall()">Search!</button>

This is Javascript:

ajaxCall()
{
    var xmlhttp = new XMLHttpRequest();

    var url = "http://localhost:8080/CSE%205335%20Project%20One/userInfo.php";

    xmlhttp.onreadystatechange=function()

    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
        {
            myFunction(xmlhttp.responseText);
        }
    }

    xmlhttp.open("GET", url, true);
    xmlhttp.send('searchdb');

    function myFunction(response)
    {
        var obj = JSON.parse(response);

        document.getElementById("democity").innerHTML =
        obj.city;

        document.getElementById("demodes").innerHTML =
        obj.description;

        document.getElementById("latlon").innerHTML =
        obj.latitude + "," + obj.longitude;

    }
}

And this is where I am trying to display the response that I am receiving from the PHP file:

<b><font size="24" face="Cambria"><p id="democity"></p></font></b>
<font size="6" face="Cambria"><p id="demodes"></p></font>
</br>

The output of the PHP file is stored in $outp and it is in the JSON format.

Any help appreciated. Thank you.

!!UPDATE!!

function ajaxCall()
{
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost:8080/CSE%205335%20Project%20One/userInfo.php";

xmlhttp.onreadystatechange=function() 
{

xmlhttp.open("GET", url, true);
xmlhttp.send('searchdb');

    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
    {
    myFunction(xmlhttp.responseText);
    }
}
}

function myFunction(response)
{
var obj = JSON.parse(response);

document.getElementById("democity").innerHTML =
obj.city;

document.getElementById("demodes").innerHTML =
obj.description;

document.getElementById("latlon").innerHTML =
obj.latitude + "," + obj.longitude;

}

This is how the improvised code looks. Still not working.

  • 写回答

2条回答 默认 最新

  • dongxing2263 2015-02-26 07:43
    关注

    Example by FactoryAidan is not going to work as it violates Same Origin Policy (unless you'll run the code in browser console on Google page). Try replacing http://www.google.com with your local address. I tested the code with a little modification and it works, or at least gives alert, so the function is called. Here's it is:

    function ajaxCall(){
        var xmlhttp = new XMLHttpRequest();
        var url = "http://localhost:8080"; /* but make sure the url is accessible and of same origin */
    
        xmlhttp.onload=function(){
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                myFunction(xmlhttp.responseText);
            }
        }
    
        xmlhttp.open("GET", url, true);
        xmlhttp.send('searchdb');
    }
    
    
    function myFunction(response){
        alert('I made it here');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测