简体   繁体   中英

HTTP Error in my POST data

This question is a continuation to this previous question of mine in here.

I have this HTTP: Error in my previous code due to query strings that is more than the limit to be send in the url to my webservice (that was what I understand). I was using method Get before and I was told by kleinohad to use method POST . Now, I am using POST but still have an error like the one I have in method GET , which is the 'HTTP: Error' everytime I have lots of post data to be send to the url, (it will not get through my webservice.).

Here is my javascript code:

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?' + $.param({path:'update/tallyHdr',json:JSON.stringify(dataJSON)}),
    dataType: primeSettings.ajaxDataType,
    success: function(data) {
      if ('error' in data)
      {
        showMessage('ERROR: ' + data["error"]["msg"]);
      }
      else{
        $('#tblTallyHdr').trigger('reloadGrid'); 
      }
    }
  });

And here is my .php code (thanks to SMka):

<?php

//this is already running code but could not accept large query string
 $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
        )
    )
));
?>

My question is, why I can't still send many data using post to my webservice? I was really thinking that using post method can help me out in sending many request to my webservice, so what's the problem with my code?

I change my code into this 我将代码更改为此

$.ajax({
        type: 'POST',
        data: dataJSON,
        url: 'processjson2.php?',
        dataType: primeSettings.ajaxDataType,
        success: function(data) {}
})

and my output url is this http://localhost/jQueryStudy/RamagalHTML/processjson2.php?path=update/tallyHdr , which give me an error Session ID not found . I found out that my datajson was not sent to my url webservice datajson was not there.

Your problem is in this line of code:

url: 'processjson2.php?' + $.param({path:'update/tallyHdr',json:JSON.stringify(dataJSON)}),

This line appends data to the URL to which you are posting your request. If you have too much data, the URL will overwhelm the web server. (Basically, you are still performing a GET request.)

Instead, replace that line with the following.

url: 'processjson2.php',    

Then add path:'update/tallyHdr' to your dataJSON object, and see if that works.

I have now my answer. Cheeken is correct when she/he said that this line:

url: 'processjson2.php?' + $.param({path:'update/tallyHdr',json:JSON.stringify(dataJSON)}),

is the problem and that I am performing a get request. And that I should replace it with this:

url: 'processjson2.php?',

So, where should I put my dataJSON? Here's my running code that I used:

$.ajax({
    type: 'POST',
    data: $.param({json:(JSON.stringify(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'); 
      }
    }
  });

This line $.param({json:(JSON.stringify(dataJSON))}) converts my dataJSON into a query string, while json is the variable that holds my json data. That's all that change I nade to run my program code with ajax

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