简体   繁体   中英

jQuery Ajax post to php not catching variable

What am i doing wrong. PHP doesn't seem to catch title and wrapper from $.ajax. Does the code look correct. The success message i get indicate an error that title is not found.

jQuery main.html

$.ajax({
   type: "POST",
   url: "process.php",
   data: 'title=test&wrapper=testing',
   success: function(msg){
     alert( "Data Saved: " + msg );
   } 
});

PHP process.php

<?php 
$title = $_REQUEST['title'];
$wrapper = $_REQUEST['wrapper'];
...
?>

Take a look: jQuery.ajax()

The data parameter is better to be a Key/Value pairs object, it's cleaner and easier to debug:)

$.ajax({
   type: "POST",
   url: "process.php",
   data: {
     title: 'test',
     wrapper: 'testing'
     },
   success: function(msg){
     alert( "Data Saved: " + msg );
   } 
});

Thats a good solution.but if I try to send data through a form in a webservice.

$.ajax({
   type: "POST",
   url: "process.php",
   data: {
     title: $('#title').val,
     name: $('#name').val
     },
   success: function(data){
     alert(data );
   } 
});

Here title and name are forms element in client side.but i am not able to get post value in json based webservice file say process.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM