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.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀