我的PHP无法将数据发布到我的网址

时间:2011-07-29 08:43:18

标签: php ajax post

我尝试搜索这个,但我找不到任何可以解决我的问题。我想生成一个像这样的URL来向我的webservice发送请求:

http://localhost/jQueryStudy/RamagalHTML/processjson3.php? path=update%2FtallyHdr&json= {"SessionID":"hHuCG3Jt1um5gV7kE320Bw7EjG97I4qZ","operation":"add", "transaction_date":"2011-7-29","supplier_id":"10000000108","wood_specie_id":"1", "lines":[{"plank_number":"7","thickness":"5","width":"8","length_t":"8","quantity":"1","board_foot":"26.67","wood_classification_id":"1","price":"15"}],"scaled_by":"WER","tallied_by":"WE","checked_by":"WE","total_bdft":"580.00","final":"N"}

这是我目前的javascript代码:

var dataJSON = {
    "SessionID": $.cookie("SessionID"),
    "operation": "add",       
    "transaction_date":$('#tallyDate').val(),
    "supplier_id":$('#supplierInput').attr("name"),
    "wood_specie_id":$('#woodSpecie').attr("name"),   
    "lines":plank_data,
    "scaled_by":$('#tallyScaled').val().toUpperCase(),
    "tallied_by":$('#tallyTallied').val().toUpperCase(),
    "checked_by":$('#tallyChecked').val().toUpperCase(),
    "total_bdft":$('#tallyTotal').val(),
    "final":"N"
  }; 
  alert('this is the datajson from add :   '  + JSON.stringify(dataJSON));

  $.ajax({
    type: 'POST',
    data: dataJSON,
    url: 'processjson2.php?path=update/tallyHdr',
    dataType: primeSettings.ajaxDataType,
    success: function(data) {
      if ('error' in data)
      {
        showMessage('ERROR: ' + data["error"]["msg"]);
      }
      else{
        $('#tblTallyHdr').trigger('reloadGrid'); 
      }
    }
  });

我的.php代码是这样的:

<?php
   $data_url = http_build_query (array('json' => $_REQUEST["json"]));
   $data_len = strlen ($data_url); 

echo file_get_contents("http://localhost:8001/" . $_REQUEST["path"], false, stream_context_create(
    array (
        'http' => array(
            'method'=>'POST',
            'header' => "Connection: close\r\nContent-Length: $data_len\r\n",
            'content'=>$data_url
        )
    )
));

Evrytime我运行我的程序,网址只有这个http://localhost/jQueryStudy/RamagalHTML/processjson2.php?path=update/tallyHdr,数据没有发布,这使得我的请求没有被执行。请帮帮我。我不知道如何修复我的PHP。

1 个答案:

答案 0 :(得分:2)

如果您希望将所有数据作为URL的一部分发送,那么您应该使用GET,而不是POST。

因此可以取消数据属性并将所有内容附加到请求URL:

$.ajax({
    type: 'GET',
    url: 'processjson2.php?path=update/tallyHdr&json='+dataJSON,
    dataType: primeSettings.ajaxDataType,
    success: function(data) {
      if ('error' in data)
      {
        showMessage('ERROR: ' + data["error"]["msg"]);
      }
      else{
        $('#tblTallyHdr').trigger('reloadGrid'); 
      }
    }
  });

如果你必须使用POST,那么你只需要提供一个变量名来与你的json一起使用:

$.ajax({
    type: 'POST',
    data: "json="+dataJSON,
    url: 'processjson2.php?path=update/tallyHdr',
    dataType: primeSettings.ajaxDataType,
    success: function(data) {
      if ('error' in data)
      {
        showMessage('ERROR: ' + data["error"]["msg"]);
      }
      else{
        $('#tblTallyHdr').trigger('reloadGrid'); 
      }
    }
  });

有关详细信息,请参阅页面末尾的示例:http://api.jquery.com/jQuery.ajax/