我试图发布一个使用Jquery的值,所以我必须使用ajax将该值传递给php页面。但是当我这样做时,我得到了未定义的索引数据错误。
这是我的代码:
$("#requirementtable tbody tr").on("click", function(){
desc = $(this).closest("tr").find("td:nth-child(4)").text().trim();
$.ajax({
type:"POST",
url:"get_diagnosis.php",
data: desc,
success: function (data, textStatus, jqXHR)
{
alert(data);
},
error: function (jqXHR, textStatus, errorThrown)
{
alert("some error occured->" + jqXHR.responseJSON);
}
})
})
这是我的PHP代码:
<?php
$data = $_POST['data'];
echo $data;
?>
答案 0 :(得分:3)
这是一个包含代码和解决方案的简单示例
//the data object
var obj={
name:"uneeb",
city:"gujranwala",
state: "punjab"
};
//the data object
//The ajax
$.ajax({
type: 'post',
url: 'ajax/dynamic_update.php',
data:object,
success: function (data) {
location.reload();
}
});
//The ajax
the PHP part
echo $_POST['name']; //prints uneeb
echo $_POST['city']; //prints gujranwala
echo $_POST['state']; //prints punjab
答案 1 :(得分:2)
检查desc
变量中是否包含某个值,您必须post
个数据,
data: {data:desc},
AJAX代码,
$.ajax({
type:"POST",
url:"get_diagnosis.php",
data: {data:desc},
success: function (data, textStatus, jqXHR) {
alert(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("some error occured->" + jqXHR.responseJSON);
}
});
在PHP中使用isset()
来防止未定义的错误,例如
<?php
$data = isset($_POST['data']) ? $_POST['data'] : 'Not defined';
echo $data;
?>
如果您需要JSON
响应格式,请在dataType:'json'
中使用$.ajax
,然后在PHP中使用json_encode()进行回复。
答案 2 :(得分:2)
替换以下内容:
from Queue import PriorityQueue
def yield_chronologically(iterators):
'iterators: list of iterator objects'
PQ = PriorityQueue()
# put first n items
for i, it in enumerate(iterators):
try:
nxt = next(it)
# this is where you have to determine the priority
# with a function get_chronological_key you have yet to write
chronological_key = get_chronological_key(nxt)
PQ.put(chronological_key, (i, nxt))
except StopIteration:
pass
# yield items and insert next item from iterator that was taken from
# into the PQ
while not PQ.empty():
_, (i, nxt) = PQ.get()
yield nxt
try:
nxt = next(iterators[i])
chronological_key = get_chronological_key(nxt)
PQ.put(chronological_key, (i, nxt))
except StopIteration:
pass
并替换为:
desc = $(this).closest("tr").find("td:nth-child(4)").text().trim();
// remove ^^^^^^^^^^ since you're currently in tr click
desc = $(this).find("td:nth-child(4)").text().trim();
使用:
data: desc,
在您的PHP代码中,您可以使用data: {data:desc}, //to pass data as object as you don't have serialized data
获取数据。
答案 3 :(得分:1)
将ajax中的data: desc,
字段替换为data:{data: desc},
,这是导致您无法在PHP代码中获取数据的原因