Javascript 从GET切换到POST
我有以下Ajax请求:Javascript 从GET切换到POST,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我有以下Ajax请求: // JavaScript function myFunc(pid) { $.ajax({ type : "GET", url : "testback.php", contentType : "application/json; charset=utf-8", dataType : "json", data : { q : "testrequest",
// JavaScript
function myFunc(pid) {
$.ajax({
type : "GET",
url : "testback.php",
contentType : "application/json; charset=utf-8",
dataType : "json",
data : {
q : "testrequest",
pid : pid
},
success : function(data) {
console.log(data)
},
error : function(jqXHR, status, error) {
console.log(status, error);
}
});
}
// PHP
require_once ("dbconnect.php");
if (isset ( $_GET ['q'] )) {
if ($_GET ['q'] == "testrequest") {
$pid = $_GET ['pid'];
$query = "SELECT * FROM `tab1` WHERE `pid` = " . $pid;
$json = array ();
if ($result = $link->query ( $query )) {
while ( $row = $result->fetch_assoc () ) {
array_push ( $json, $row );
}
}
header ( "Content-type: application/json; charset=utf-8" );
die ( json_encode ( $json ) );
exit ();
}
die ();
}
它向MySQL数据库发送一个请求,并返回预期的输出
但是,我现在想切换到POST
,而不是GET
当我用POST交换GET时:
// JavaScript
function myFunc(pid) {
$.ajax({
type : "POST", // POST
url : "testback.php",
contentType : "application/json; charset=utf-8",
dataType : "json",
data : {
q : "testrequest",
pid : pid
},
success : function(data) {
console.log(data)
},
error : function(jqXHR, status, error) {
console.log(status, error);
}
});
}
// PHP
require_once ("dbconnect.php");
if (isset ( $_POST ['q'] )) { // POST
if ($_POST ['q'] == "testrequest") { // POST
$pid = $_POST ['pid']; // POST
$query = "SELECT * FROM `tab1` WHERE `pid` = " . $pid;
$json = array ();
if ($result = $link->query ( $query )) {
while ( $row = $result->fetch_assoc () ) {
array_push ( $json, $row );
}
}
header ( "Content-type: application/json; charset=utf-8" );
die ( json_encode ( $json ) );
exit ();
}
die ();
}
我在控制台中遇到以下错误:
parsererror SyntaxError:JSON输入意外结束
请求负载仍然是q=testrequest&pid=1
为了从
GET
切换到POST
,我还需要更改什么?在Ajax函数中,您需要省略内容类型,因为它已经在Ajax调用中定义。删除行“contentType:”application/json;字符集=utf-8”,如下所示:
$.ajax({
type : "GET", // Or POST
url : "testback.php",
contentType : "application/json; charset=utf-8", // REMOVE THIS LINE!!
dataType : "json",
data : {
q : "testrequest",
pid : pid
},
success : function(data) {
console.log(data)
},
error : function(jqXHR, status, error) {
console.log(status, error);
}
});
在那之后它应该可以正常工作!
干杯!美元.ajax有效,但我建议改用美元.get或美元.post。 当您使用$.get时,请注意您的url。浏览器有字符限制(大约2000个字符)
if(urlGet.length<2000){
//使用$获得少于2000克拉的热量。获得
$.get(urlGet,函数(){
}).完成(功能(数据){
//好的,用返回的数据做一些事情
}).fail(函数(){
//错误
});
}否则{
//使用$.POST参数:{name:“John”,age:“25”}
$.post(urlPost,{name:“John”,age:“25”},函数(){
}).done(函数(){
//嗯
}).fail(函数(){
//失败
});
}
您可以使用此代码创建一个函数,然后对您的Web服务进行简单调用
以下是jQuery doucmentation:
美元
美元邮政
希望有帮助;)使用
json\u decode()
在您的$\u帖子中query@ixe:您能告诉我具体位置吗?@user1170330删除contentType
和dataType
settings@postrel:现在,我只得到一个空响应。没有任何错误或其他数据。使用$\u请求如果您不知道哪种提交方法,它将同时接收get和POST数据。您将获得此结果错误,因为JSON的格式无效。
if (urlGet.length < 2000) {
// GET less than 2000 caractères use $.GET
$.get(urlGet, function () {
}).done(function (data) {
//OK do stuff with data returned
}).fail(function () {
//err
});
} else {
//Use $.POST params : { name: "John", age: "25" }
$.post(urlPost, { name: "John", age: "25" }, function () {
}).done(function () {
//ok
}).fail(function () {
//fail
});
}