weixin_33691700 2013-06-22 08:50 采纳率: 0%
浏览 35

无法使用Ajax访问php

I've got the Problem that I can't reach my php file with my $.ajax() call. I always get a jqXHR.status of 0. This is my code:

function with $.ajax() call:

function updateClubInfo(text) {
    var data = text;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '../php/updateInfo.php',
        data: {
            infoText: data,
            action: "update"
        },
        success: function() {
            // do nothing
            alert('success');
        },
        error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.
 Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.
' + jqXHR.responseText);
            }
        }
    });
}

I've build this request similar to others in my project, but this one is the only one that doesn't work. Here the PHP file I want to reach:

php code snippet (updateInfo.php):

<?php
    $action = $_POST['action'];
    $text = $_POST['data'];

    myLog($action);
    myLog($text);

    echo "hello";


    /*
     * Writes entry in Logfile
     */
    function myLog($data) {
        $text = @file_get_contents('log.txt');
        $text = $text . "
" . $data;
        @file_put_contents('log.txt', $text);
    }   
?>

When I try to reach this PHP file in the URI the echo is outputted. So I think the Problem should be in the ajax call.

Does someone has an Idea what I'm doing wrong? I'm grateful for any help.

Thx alot for your help Cris

  • 写回答

2条回答 默认 最新

  • 7*4 2013-06-22 08:53
    关注

    You have a copy/paste error in your PHP file. It should be :

    $action = $_POST['action'];
    $text = $_POST['infoText'];//instead of $_POST['data']
    

    UPDATE

    Because your AJAX request asks for JSON data, but you are writing only text in your PHP file. Therefore, the AJAX request interprets the answer as void, then HTTP status = 0

    Solution

    dataType option is not about the type of data you are sending to the server, but the type or data you are expecting back from the server. Change dataType: 'json', to dataType: 'html', or symply remove it to let JQuery chose the appropriate mode.

    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题