没有ajax call怎么办,jQuery ajax调用失败(jQuery ajax call failing)

jQuery ajax调用失败(jQuery ajax call failing)

我在使用jQuery AJAX调用时遇到了一些问题。 我不是专家,但我知道jQuery足以完成简单的任务。

这是我的代码 -

var id = 123;

var url = "/test.aspx?id=" + id;

$.getJSON(url, function (data) {

alert(1);

});

问题是警报(1)没有被击中。 当我手动检查[domain] /test.aspx?id=123时,我正在获取JSON数据。 我在这里做错了什么,或者我如何在这里诊断问题?

谢谢

I'm having some issues with jQuery AJAX calls. I'm by no means an expert but I know jQuery enough to get by with simple tasks.

Here is my code -

var id = 123;

var url = "/test.aspx?id=" + id;

$.getJSON(url, function (data) {

alert(1);

});

The problem is that the alert(1) is not getting hit. When I check for [domain]/test.aspx?id=123 manually, I'm getting JSON data back. What am I doing wrong here or how can I diagnose the issue here?

Thanks

原文:https://stackoverflow.com/questions/17995667

更新时间:2019-12-25 09:33

最满意答案

你的例子非常好。 这是一个测试它的JSfiddle示例(使用echo服务):

var id = 123;

var url = "/echo/json/";

$.getJSON(url, function (data) {

alert("hello");

});

It's a little strange but I had to specify a data type of JSON in the request.

var id = 123; var url = "/echo/json/"; $.getJSON(url, "json", function (data) { alert("hello"); });

I got the idea of JSON from http://api.jquery.com/jQuery.getJSON/. The example on this page was for PlainObject. The url in my code was trying to parse JSON as PlainObject but failing. I used .success and .error after closing bracket of getJSON to figure out the issue. To see how success and error can be used scroll to the bottom of the jquery.com link.

相关问答

修正了,我从application/json; charset=utf8更改了content-type application/json; charset=utf8 application/json; charset=utf8只是application/json 。 我讨厌IE :) 还要避免IE超级缓存尝试这个: var d = new Date();

$.ajax({

url:"{{SITE_URL}}/content/twitter.json?_="+d.getTime(),

...

由于相同的原始策略,此常规JSON调用将不起作用。 这是您的错误告诉您的内容: is not allowed by Access-Control-Allow-Origin 。 正确的JSONP语法是: $.ajax({

url: 'http://www.geognos.com/api/en/countries/info/all.jsonp',

dataType: "jsonp",

jsonpCallback: 'callback',

success: functio

...

您提供的代码似乎有用。 我甚至用jQuery 1.12.1在本地测试它并且没有任何问题。 您收到的错误表示ajax调用在完成之前被中断。 有很多事情可以导致这种情况。 最常见的包括跨域问题以及与您的ajax调用一起使用链接或表单操作。 有关详细信息,请参阅以下文章: http://www.justinball.com/2013/02/25/jqxhr-returning-readystate-0-and-status-0/ 快速谷歌搜索: {readyState:0,responseText:“”

...

你的例子非常好。 这是一个测试它的JSfiddle示例(使用echo服务): var id = 123;

var url = "/echo/json/";

$.getJSON(url, function (data) {

alert("hello");

});

http://jsfiddle.net/QyfCF/1/ It's a little strange but I had to specify a data type of JSON in the request. var id

...

我认为问题出在这里,但我没有使用AJAX一段时间,所以我无法完全通过AJAX代码: echo $return['ReturnString'];

return json_encode($return); 你应该回应json_encode($return); echo json_encode($return); 这应该有希望解决它。 虽然我不知道你为什么要循环这些数据100次......但是。 I think the problem lies here, but I have not used AJA

...

所以我通过做了很多测试来解决这两个问题。 jQuery一个很容易,更新到jQuery 2.1.4解决了它,我不想知道是什么导致这个,它现在才起作用 。 对于第二个问题,我将超时减少到500毫秒,然后遇到超时,这导致我的onreadystate被调用,即使请求没有完成(在获得及时的答案方面),因此获得status = 0 。 为什么会发生这种情况,见上文。 为了确保超时不仅仅是“随机”,我添加了一个重试计数器并修改了我的Ajax函数: function ajax(options) {

var

...

我对我有点生疏,但我认为你已经忘记了AJAX是异步的,这意味着直到后来发生的事情才会发生。 因此,在您的示例中,您已完成AJAX调用,然后检查了您的会话,然后调用完成了其功能(由于发送的数据量较大,需要更长的时间)。 这就是它在刷新后看到新值的原因。 你有3个选择: 在您的success函数中包含//do something语句。 代码示例: $.post(ajaxurl, data, function (response) {

alert("Account Created");

...

确保您已经配置了要从脚本客户端调用的服务:在服务中的web.config中添加几个元素:

更多信息: http : //msdn.microsoft.com/en-us/library/bb763177(v = vs。90).asp

...

也许你可以添加dataType参数(将“text”作为数据类型)传递给你的.ajax调用...当得到格式错误的XML或JSON时,我通常会得到一个parsererror 。 如果未设置dataType ,jquery会尝试自动确定响应是XML还是HTML。 也许这是失败的,因为你的回答都不是? 请参见http://docs.jquery.com/Ajax/jQuery.ajax#options Perhaps you could add the dataType parameter (passin

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值