2016-06-24 21 views
1

Ich habe folgende Ajax-Anfrage:Umschalten von GET POST

// 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(); 
} 

Es hat eine Anfrage an meine MySQL-Datenbank sendet und gibt die erwartete Ausgabe.

Allerdings möchte ich jetzt auf POST statt GET wechseln.
Wenn ich tauschen nur mit 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(); 
} 

ich folgende Fehlermeldung in der Konsole:

parsererror SyntaxError: Unexpected end of JSON input

Die Anfrage Nutzlast ist noch q=testrequest&pid=1.

Was muss ich noch ändern, um von GET zu POST zu wechseln?

+0

Verwendung 'json_decode()' in Ihrer _POST Abfrage $ – ixe

+0

@ ixe: Kannst du mir sagen, wo genau? – user1170330

+0

@ user1170330 Einstellungen 'contentType' und' dataType' entfernen – postrel

Antwort

1

In Ihrer Ajax-Funktion müssen Sie den Inhaltstyp weglassen, wie er bereits im Ajax-Aufruf definiert ist. Löschen Sie die Zeile "contentType:" application/json;

$.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); 
    } 
}); 

Es danach ganz gut sollte Prost arbeiten

0

Die $ Schnipsel funktioniert, aber ich rate $ .get oder $ .post stattdessen zu verwenden: charset = utf-8" weiter unten!. Sei carrefull über Ihre uRL, wenn Sie $ verwenden .get, Browser haben caracter Grenzen (etwa 2000 caracters).

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 
    }); 
} 

Sie eine Funktion mit diesem Code erstellen und dann einen einfachen Anruf Ihres WebServices.

Hier

ist die jQuery doucmentation:

.get $ https://api.jquery.com/jquery.get/

.post $ https://api.jquery.com/jquery.post/

Hoffnung diese Hilfe;)