dsf4354353452 2018-09-01 20:38
浏览 36
已采纳

JQuery成功导致 字符

I have these characters in my results.
The same code works fine on another page.

This is my PHP:

// Insert Data $queryData = $db->prepare("INSERT INTO tbl_Category (Status, Parent, Name) VALUES (?, ?, ?)");

$insertData = $queryData->execute (
    array (
        $Status,
        $Parent,
        $Name,
    )
);

if(insertData) { echo "success"; } else { echo "error"; }

This is my JQuery:

$.ajax({
    type:'POST',
    url:"ajax/Category.php?ajaxPost=newCategory",
    data:$('form#frmAction').serialize(),
    success: function(result){
        if(result == "success")
        {
            alert("ok ");
        }
        else if(result == "error")
        {

        alert("püff");
        }
    }
});

This is what the Firefox Debugger shows

And my result is:

result = "success
" 
  • 写回答

2条回答 默认 最新

  • dousi7919 2018-09-01 20:56
    关注

    refer to the end of the string characters. Interacting between JavaScript and PHP using ajax is usually implemented using json, which is a data format supported widely by all programming languages.

    You may refactor your code as follows. The die() function is used to ensure no extra output is printed by the PHP script, since this would result in invalid json syntax.

    category.php

    if($insertData) { 
        die(json_encode(['result' => true])); 
    } else { 
        die(json_encode(['result' => false, 'errorMessage' => 'Optional error message goes here.'])); 
    }
    

    jQuery

    Note: I have used the $.post() function, this is a shorthand for $.ajax(type: 'POST').

    $.post('ajax/Category.php?ajaxPost=newCategory', $('form#frmAction').serialize(), function(r) {
        if(r.result) {
            alert("OK");
        } else {
            alert("Something went wrong: " + r.errorMessage);
        }
    }, 'json');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度