[英]jQuery AJAX call not returning data
I feel a little silly here. 我在这里有点傻。 My ajax call is running the error: function every time.
我的ajax调用每次都运行error:函数。 I know that the data is coming back as JSON, but i've done the datatype as jsonp to allow for cross origin stuff.
我知道数据以JSON的形式返回,但是我已经将数据类型作为jsonp进行了处理,以允许跨源的东西。 I don't think I can do anything differently, unless I'm forgetting something obvious.
我认为我无法做任何其他事情,除非我忘记了一些显而易见的事情。 Please- whats wrong with this:
请-这有什么问题:
function sartleApi(type,endpoint,object,callback){
$.ajax({
contentType: "application/json",
dataType: 'jsonp',
type:type,
data:object,
url:"http://dev.sartle.com/includes/ajax_reviewcomment.php?rid=1178",
success:function(data){
callback(data);
},
error: function (xhr, textStatus, errorThrown) {
alert(xhr.statusText);
alert(xhr.responseText);
alert(xhr.status);
alert(errorThrown);
}
});
}
Your website doesn't support JSONP. 您的网站不支持JSONP。
JSONP is just a fancy way of passing a JSON object to a global callback function via a <script>
tag. JSONP只是通过
<script>
标记将JSON对象传递给全局回调函数的一种奇特的方式。 It circumvents cross-origin restrictions by not sending an AJAX request in the first place, but instead creating a <script>
tag. 它不首先发送AJAX请求,而是创建一个
<script>
标签,从而规避了跨域限制。
A JSON response looks like this: JSON响应如下所示:
{"foo": "bar"}
But a JSONP response is: 但是JSONP响应是:
some_callback({"foo": "bar"})
That PHP script doesn't wrap the JSON response in a callback function (whose name is usually specified via the callback
GET parameter), so you simply can't make a JSONP request. 该PHP脚本不会将JSON响应包装在回调函数中(后者的名称通常是通过
callback
GET参数指定的),因此您根本无法发出JSONP请求。 The request will succeed, but the global callback function will not be called, so you will not be able to use the JSON. 该请求将成功,但是不会调用全局回调函数,因此您将无法使用JSON。
stlll, it seems a cross-domain issue: please try this library https://github.com/padolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js stlll,这似乎是跨域问题:请尝试使用此库https://github.com/padolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js
as a jsonp
the result is already a callback function with a javascript object argument. 作为
jsonp
,结果已经是带有javascript对象参数的回调函数。 Make sure that the returned function by the server is implemented: The server could return my_callback({...})
. 确保实现了服务器返回的功能:服务器可以返回
my_callback({...})
。 You need to implement my_callback
function client-side. 您需要在客户端实现
my_callback
函数。
place some alerts/console.log on both success
and error
functions. 在
success
和error
功能上放置一些alert / console.log。 Always make a little debug of your own before posting the issue. 发布问题前,请务必先进行一些调试。
as posted in the comment: state the return code of the ajax call. 如评论中所述:说明ajax调用的返回代码。
So, to try to circumvent the cross domain issue, using jSONP turned out to be a bad idea. 因此,试图规避跨域问题,事实证明使用jSONP是一个坏主意。 I'm running these calls from a localhost, so i've changed the url in the ajax call to
我正在从本地主机运行这些调用,所以我将ajax调用中的网址更改为
url:"http://localhost/includes/ajax_reviewcomment.php?rid=1178"
I will build this url to be dynamic so that the current URL always is domain-consistent with the server, and i should be in good shape! 我将这个URL构建为动态的,以便当前URL始终与服务器域一致,并且我应该处于良好状态!
in type you need put "GET" or "POST" 在类型中,您需要输入“ GET”或“ POST”
$.ajax({
contentType: "application/json",
dataType: 'jsonp',
type:type, <<<<<<------ here
data:object,
url:"http://dev.sartle.com/includes/ajax_reviewcomment.php?rid=1178",
success:function(data){
callback(data);
},
error: function (xhr, textStatus, errorThrown) {
alert(xhr.statusText);
alert(xhr.responseText);
alert(xhr.status);
alert(errorThrown);
}
}); });
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.