美元。针对简单的php, post返回对象
16lz
2021-01-22
I guess I'm just having difficulty with $.post... I've used it successfully in the past, but I have to be forgetting something important. All I want is a "hello world", but I'm getting a complicated object. I am befuddled.
我想我只是对$.post有困难。我曾经成功地使用过它,但我不得不忘记一些重要的东西。我只想要一个“你好世界”,但我得到了一个复杂的对象。我糊里糊涂的。
var_test.php
var_test.php
<?php
PRINT "hi";
?>
index.html
index . html
<!doctype html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<html><head>
var thing = $.post( "var_test.php");
console.log(thing);
</head><body></body></html>
console output
控制台输出
Object { readyState: 1, getResponseHeader: .ajax/v.getResponseHeader(), getAllResponseHeaders: .ajax/v.getAllResponseHeaders(), setRequestHeader: .ajax/v.setRequestHeader(), overrideMimeType: .ajax/v.overrideMimeType(), statusCode: .ajax/v.statusCode(), abort: .ajax/v.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 8 more… }
3 个解决方案
#1
1
You should add a callback instead that receives the response/data from your php:
您应该添加一个回调函数来接收来自php的响应/数据:
$.post( "var_test.php", function(response) {
console.log(response);
});
That callback will be executed once the request has finished, that way, you can be sure that you will have your data ready and set to use it in your JS.
该回调将在请求完成后执行,这样,您就可以确保准备好数据并将其设置为在JS中使用。
更多相关文章
- 开发学习Jquery实例---双击表格弹出模态对话框,编辑提交数据
- 执行Django数据库值的计算:视图。py或Javascript模板吗?
- jquery 如何获取json指定键为指定值的另一个指定键的值,该值还是
- Jquery 传json格式数据到后台,后台反序列化得到一个集合list
- JQuery.AJAX - 我的服务器可以返回一大块数据吗?
- jQuery向服务器请求数据并处理
- jquery datatables-一个数据表中的最大列数
- 前台jquery+ajax向后台请求数据,后台返回json,前台显示
- 使用js和ajax从django服务器检索数据