2014-12-30 180 views
0

我试图从php文件中检索一行代码。我的代码如下:

$(document).ready(function() { 
    alert("1"); 
    $.get('http://myurl.com/toolbox/myPHP.php?store_id=4649b4a8a802c3&couponShow=checkout', function(data) { 
     alert(data); 
     alert("2"); 
    }); 
}); 

window.onload = function() { 
    if (window.jQuery) { 
     // jQuery is loaded 
     alert("Yeah!"); 
    } else { 
     // jQuery is not loaded 
     alert("Doesn't Work"); 
    } 
} 

"1""Yeah!"适当警告,所以jQuery是加载,但我不能get函数里面搞定。访问里面的url返回正是预期的。我在这里做什么有什么问题吗?我可以采取什么步骤来发现问题?

+1

您是否试图获取跨域的内容?检查开发工具控制台是否有错误。 – PeterKA

+0

或来自硬盘。同样的问题 – mplungjan

+0

是的,在服务器上允许跨域。 – Christian

回答

0

在请求结束时尝试使用.fail()来确定它是否有关于php文件的错误。

使用data回调接收失败的请求的详细信息

这将工作:

$.get("http://api.postcodes.io/postcodes?q=da117ne", function (data) { 
    alert("it worked: " + data.status); 
}).fail(function (data) { 
    alert(data.statusText); 
}); 

这不会,但会给出错误

的响应
$.get("dedededhttp://api.postcodes.io/postcodes?q=da117ne", function (data) { 
    alert("it worked: " + data.status); 
}).fail(function (data) { 
    alert(data.statusText); 
}); 

这是jsfiddle的一个例子: http://jsfiddle.net/rx6ws7hw/

+0

一个例子会使这个更好的答案。事实上,这几乎只是一条评论或建议。 –

+0

谢谢@JonathanKuhn我提供了代码和jsfiddle。 –

0
$(document).ready(function() { 
    alert("1"); 
    $.get('http://myurl.com/toolbox/myPHP.php?store_id=4649b4a8a802c3&couponShow=checkout') 
    .done(function() { 
     alert("2"); 
    }).fail(function(){ 
     alert("3") ; 
    }); 
}); 

See Jquery $.Get

+0

谢谢,我不确定问题是什么,但它似乎突然间开始工作。 – Christian

0

我试图复制/粘贴在当前页面上稍作修改的版本是这样的(它的工作):

$(document).ready(function() { 
    $.get('http://stackoverflow.com', function(data) { 
     window.console.log(data); 
    }); 
}); 

如果您想尝试一下你可以做同样的事情。

在此页面的Chrome中,按下Command + Option + I(菜单 - >更多工具 - >开发人员工具)。点击出现的这个新“开发者窗格”顶部的控制台标签。将上面的代码粘贴到控制台中并按下回车键。

但是,如果我改变它咯,

$(document).ready(function() { 
    $.get('http://google.com', function(data) { 
     window.console.log(data); 
    }); 
}); 

我得到这个(不添加用于调试.fail):

XMLHttpRequest cannot load http://google.com/?_=1419962724028. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://stackoverflow.com' is therefore not allowed access. 

我不是超级熟悉的通过交叉抓取内容域,但也许在你自己的服务器上使用这个小测试将会使你朝着正确的方向发展。

我认为我们需要更多信息才能更全面地诊断问题。

我很抱歉,如果你已经知道所有这些:)我知道这很简单,但它是我开始的第一个地方。

相关问题