DragonWar% 2015-08-06 21:14 采纳率: 0%
浏览 1

jQuery Ajax无法正常工作

This is my function to call ajax:

var sendAjax = function(type, data, callback) {
  $.ajax({
    url: homepage + 'server_ajax.php',
    type: 'POST',
    data: {
      type: type,
      data: JSON.stringify(data)
    }
  }).done(function(data) {
    console.log(data);
    callback(true, JSON.parse(data));
  }).fail(function() {
    console.log('NO');
    callback(false, {});
  });
};
  1. Variable homepage contains valid URL. It is same domain, so this is not the problem.

  2. My server_ajax.php has only this: echo "awdawda wawddawawd"; - nothing more (testing purposes).

  3. I tried to put breakpoints to $.ajax... and to the console logs. First breakpoint works, but functions done and fail never run.

  4. No error show up in the browser console

  5. My jQuery is include (I'm sure), I use require.js and everything else in this JS file using jQuery works well.

  6. I also tried change URL to something invalid (like server_ajax2.php) and console prints error 404 - like it would.

  7. Also tried with dataType: 'json' or removing data object...

Any ideas, what should I try? I also tried some answers on this site (don'r flag this as duplicate, I tried like everything), but nothing helped me.

  • 写回答

2条回答 默认 最新

  • derek5. 2015-08-06 21:26
    关注

    Have you tried doing the following on the server_ajax.php file:

    echo json_encode('awdawda wawddawawd');
    header('X-PHP-Response-Code: 200', true, 200);
    

    This encodes the response as JSON so that your JavaScript code can read the content, and gives the standard success header (200 OK) to the page so that the $.ajax code knows the query was successful.

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝批量检测实名工具
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题