jQuery Ajax简单调用

我正在尝试一个基本的ajax调用。 所以我在测试服务器上托管以下测试php: http: //voicebunny.comeze.com/index.php?numberOfWords = 10这个网页是我自己的测试,已经集成到VoiceBunny API http:// voicebunny.com/developers 。

现在我需要使用jQuery在其他网页上获取该网页打印的数据。 正如您所看到的,网页echo是一些JSON。 如何从其他网页获取此JSON?

这是我的代码:

$.ajax({ 'url' : 'http://voicebunny.comeze.com/index.php', 'type' : 'GET', 'data' : { 'numberOfWords' : 10 }, 'success' : function(data) { alert('Data: '+data); }, 'error' : function(request,error) { alert("Request: "+JSON.stringify(request)); } }); 

我尝试了很多其他的变种,但我总是得到一个错误,而不是JSON。 谢谢

请在你的ajax调用中设置dataType配置属性,再试一次吧!

另一点是你使用ajax调用设置配置属性作为字符串,它作为参考站点是错误的

 $.ajax({ url : 'http://voicebunny.comeze.com/index.php', type : 'GET', data : { 'numberOfWords' : 10 }, dataType:'json', success : function(data) { alert('Data: '+data); }, error : function(request,error) { alert("Request: "+JSON.stringify(request)); } }); 

希望对你有所帮助!

您还可以使ajax调用更通用,可重用,因此您可以从不同的CRUD(创建,读取,更新,删除)任务中调用它,并从这些调用中处理成功案例。

 makePostCall = function (url, data) { // here the data and url are not hardcoded anymore var json_data = JSON.stringify(data); return $.ajax({ type: "POST", url: url, data: json_data, dataType: "json", contentType: "application/json;charset=utf-8" }); } // and here a call example makePostCall("index.php?action=READUSERS", {'city' : 'Tokio'}) .success(function(data){ // treat the READUSERS data returned }) .fail(function(sender, message, details){ alert("Sorry, something went wrong!"); });