Javascript 将函数添加到成功选项时,Ajax请求不起作用
我在获取ajax GET请求(或任何与此相关的请求)以检索响应时遇到问题。我只是尝试在警报事件中返回响应:Javascript 将函数添加到成功选项时,Ajax请求不起作用,javascript,jquery,ajax,Javascript,Jquery,Ajax,我在获取ajax GET请求(或任何与此相关的请求)以检索响应时遇到问题。我只是尝试在警报事件中返回响应: <script> $(document).ready(function() { $('#test').click(function() { $.ajax ({ type: 'Get', url: 'https://crm.zoho.com/crm/privat
<script>
$(document).ready(function() {
$('#test').click(function() {
$.ajax ({
type: 'Get',
url: 'https://crm.zoho.com/crm/private/json/Potentials/searchRecords?authtoken=XXX&scope=crmapi&criteria=(((Potential Email:test@email.com))&selectColumns=Potentials(Potential Name)&fromIndex=1&toIndex=1',
dataType: 'json',
success: function(data) {
alert(data);
}
});
});
});
</script>
$(文档).ready(函数(){
$(“#测试”)。单击(函数(){
$.ajax({
键入:“Get”,
网址:'https://crm.zoho.com/crm/private/json/Potentials/searchRecords?authtoken=XXX&scope=crmapi&criteria=((潜在电子邮件:test@email.com))&选择Columns=Potentials(Potential Name)&fromIndex=1&toIndex=1',
数据类型:“json”,
成功:功能(数据){
警报(数据);
}
});
});
});
我可以通过取消success选项中的函数并编辑如下代码来实现此和其他类似的post请求:
<script>
$(document).ready(function() {
$('#test').click(function() {
$.ajax ({
type: 'Get',
url: 'https://crm.zoho.com/crm/private/json/Potentials/searchRecords?authtoken=XXXX&scope=crmapi&criteria=(((Potential Email:test@email.com))&selectColumns=Potentials(Potential Name)&fromIndex=1&toIndex=1',
dataType: 'json',
success: alert('success')
});
});
});
</script>
$(文档).ready(函数(){
$(“#测试”)。单击(函数(){
$.ajax({
键入:“Get”,
网址:'https://crm.zoho.com/crm/private/json/Potentials/searchRecords?authtoken=XXXX&scope=crmapi&criteria=((潜在电子邮件:test@email.com))&选择Columns=Potentials(Potential Name)&fromIndex=1&toIndex=1',
数据类型:“json”,
成功:警报(“成功”)
});
});
});
这是为什么?更重要的是,我如何检索响应数据并将其传输到警报消息?非常感谢您的帮助
**更新:
在阅读前两位用户对这个问题的回答后,我得到的是:
<script>
$(document).ready(function() {
$('#test').click(function() {
$.ajax ({
type: 'GET',
url: 'https://crm.zoho.com/crm/private/json/Potentials/searchRecords?authtoken=418431ea64141079860d96c85ee41916&scope=crmapi&criteria=(((Potential%20Email:test@email.com))&selectColumns=Potentials(Potential%20Name)&fromIndex=1&toIndex=1',
dataType: 'json',
success: function(data) {
alert(JSON.stringify(data));
},
error: function(data) {
alert(JSON.stringify(data));
}
});
});
});
</script>
$(文档).ready(函数(){
$(“#测试”)。单击(函数(){
$.ajax({
键入:“GET”,
网址:'https://crm.zoho.com/crm/private/json/Potentials/searchRecords?authtoken=418431ea64141079860d96c85ee41916&scope=crmapi&criteria=((潜在的%20电子邮件:test@email.com))&选择Columns=Potentials(Potential%20Name)&fromIndex=1&toIndex=1',
数据类型:“json”,
成功:功能(数据){
警报(JSON.stringify(数据));
},
错误:函数(数据){
警报(JSON.stringify(数据));
}
});
});
});
我能够得到错误响应,因此我可以确认存在某种错误。我还想指出,我是从另一个域(不是crm.zoho.com)发出请求的,所以我应该使用jsonp吗?如果是,我将如何更改代码?
success: alert('success')
如果没有成功的请求,您实际上是在AJAX方法的开头执行此函数。success
参数需要指向函数的指针,当您使用alert('success')
时,您是在执行函数,而不是提供指向函数的指针
您需要尝试的第一件事是将type更新为GET
,而不是GET
:
$.ajax ({
type: 'GET',
尝试使用.done()函数,如下所示:
<script>
$(document).ready(function() {
$('#test').click(function() {
$.ajax ({
type: 'Get',
url: 'yourUrl',
dataType: 'json',
}
}).done(function(result) {alert(data);}) //try adding:
.error(function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);})
});
});
$(文档).ready(函数(){
$(“#测试”)。单击(函数(){
$.ajax({
键入:“Get”,
url:“你的url”,
数据类型:“json”,
}
}).done(函数(结果){alert(data);})//尝试添加:
.error(函数(jqXHR、textStatus、errorshown){
console.log(textStatus,errorshown);})
});
});
error函数还将在控制台中为您提供有关请求状态的一些信息。首先更正
类型:“GET”
,然后关注第一个正确的代码段。但是由于您有数据类型:“json”
,成功回调方法的数据参数将有一个json对象。因此,您可以如果调用成功,我只会在警报中看到“Object”,所以请尝试alert(JSON.stringify(data))
来修复它。但是如果失败,请添加fail:function(data){}
方法,并在其中执行相同的alert(JSON.stringify(data))
。