最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Ajax post to php not catching variable - Stack Overflow

programmeradmin2浏览0评论

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'];
...
?>

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'];
...
?>
Share Improve this question asked Jun 29, 2011 at 8:00 PinkiePinkie 10.2k22 gold badges81 silver badges124 bronze badges 2
  • add print_r($_REQUEST) to see what's ming in. Also try replacing it with $_POST – Maxim Krizhanovsky Commented Jun 29, 2011 at 8:05
  • I've just checked your js code and it works. my process.php: <?php $a = $_REQUEST['title']; $b = $_REQUEST['wrapper']; echo "title: $a, wrapper: $b"; ?> – Kamil Commented Jun 29, 2011 at 8:27
Add a ment  | 

2 Answers 2

Reset to default 6

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

发布评论

评论列表(0)

  1. 暂无评论