donglingyi4679 2014-02-26 10:53
浏览 33

AJAX返回完整的HTML站点而不是JSON对象

I finally could receive a response from my AJAX PHP call. But now in return I get my full HTML site instead of a JSON object or string. What is wrong here?

var request = $.ajax({
  url: "mysite.php",
  type: "POST",
  data: {select:requestStr},
  dataType: "html"
});

request.done(function( data ) {
  console.log(JSON.stringify(data));
});

I send a simple string to my php class. This is what I get from the response-text in my developer tool from the browser:

data=Test

On PHP site I just return that respone:

<?php
    $myData = array();
    $myData['data'] = "test";
    if (isset($_POST)) {
        $myData['data'] = $_POST;
    }
    echo json_encode($myData);
    exit();
?>

And this is the console.log from the response:

"<!DOCTYPE html>
<html>
<head> ... </html>\"Test\""

EDIT

I only need the end of the response and that is "Test" but not the whole HTML file.

UPDATE I extracted my PHP class and wrote a Little Version like the PHP code above. But now my response is an empty object {"data":[]}

  • 写回答

2条回答 默认 最新

  • duanmei2805 2014-02-26 10:55
    关注

    Put a exit(); after echo json_encode($myData);

    评论

报告相同问题?

悬赏问题

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