Can't get data from AJAX response in PHP - php

I just started programming but I don't know how to get this POST response into a PHP file,
Request URL: http://localhost/getEmployees.php
Request Method: POST
Status Code: 200 OK
Remote Address: 127.0.0.1:63342
Referrer Policy: no-referrer-when-downgrade
access-control-allow-credentials: true
access-control-allow-origin: http://localhost:63342
content-length: 4850
Content-type: text/html; charset=UTF-8
server: PhpStorm 2020.1
vary: origin
X-Powered-By: PHP/7.3.6
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,es-US;q=0.8,es;q=0.7,sm;q=0.6
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 105
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: Phpstorm-7e92279c=4f7ff7c6-1a41-4c9e-90dd-dcc6680dcb42; loginAuthorised=loginAuthorised; companyId=1; userId=10; userName=Claudia+Najera; PHPSESSID=nequ49622cdstv6oh0ahsmis10
Host: localhost:63342
Origin: http://localhost:63342
Pragma: no-cache
Referer: http://localhost:63342/htdocs/flavorite.io/tools/tests.php?_ijt=buaa7pmm8icavombffnis1htfu
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36
X-Requested-With: XMLHttpRequest
query[generalSearch]: Mario
selectedAllRows: false
requestIds: true
sort[field]:
sort[sort]: asc
What I am trying to do is get query[generalSearch] string. I have in another PHP file to fet the results:
$dbConnection = connection();
$generalSearchJSON = #$_POST['generalSearch'];
$generalSearch = json_decode($generalSearchJSON);
I want to use the $generalSearch variable to search the database. But it doesn't seem to get the result. Any suggestions?

You have a typo.Remove the # from $_POST['generalSearch'];
So you'll get this:
$dbConnection = connection();
$generalSearchJSON = $_POST['generalSearch'];
$generalSearch = json_decode($generalSearchJSON);

Related

how to check if i am having correct file at laravel controller from post request

trying to upload an image at laravel: On echo $request; i am getting this: POST /infoshore0/api/public/v1/product/addProductImg?lang=en-us HTTP/1.1 Accept: application/json, text/plain, */* Accept-Encoding: gzip, deflate, br Accept-Language: en-IN,en-GB;q=0.9,en-US;q=0.8,en;q=0.7 Authorization: Bearer eyJ0eXAiOiJKV1QiLNjc0MTcsIm5iZiI6MTYwMjI2MzgxNywianRpIjoiZmxhdHJ1Mk5BeGdLc0JWRCJ9.lFcZOOSZx_94eZY97L-OfR36XLX_KLqvbZFJo5l2FW8 Connection: keep-alive Content-Length: 209141 Content-Type: text/plain;charset=UTF-8 Cookie: laravel_session=7dab0a4fdba93fbec911f Host: localhost:8080 Origin: http://localhost:8080 Referer: http://localhost:8080/cp/ Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors Sec-Fetch-Site: same-origin User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36 {"file":"ÿØÿà\u0000\u00.......very long text","product_id":499859489}
I have tried if ($request->hasFile('file')) { echo 'File Found'; } and nithing is happeningAlso Passing product_id and image file using angularJS

I get a 404 using the integrated server when POSTing to a php script

<script>
$.post("api.php", {action: 'check_login'}, function (response) {
if (response.status == 'success') {
$('#loginForm').hide();
$('#loginHeader').text('You are already logged in.');
setTimeout(function () {
alert('You will now be redirected.');
$(window.location).attr("href", "http://example.com");
}, 3000);
}
}, "JSON");
</script>
The above jQuery script POSTs this request to a php script in the same folder:
POST http://localhost:63342/100D100/api.php HTTP/1.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
Referer: http://localhost:63342/100D100/login.html?_ijt=taksdipisi3m6dq085nom202e2
Accept-Language: it-IT
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Length: 18
Host: localhost:63342
Connection: Keep-Alive
Pragma: no-cache
Cookie: Phpstorm-b9495478=581c317c-7b73-4419-92d8-ab238525aac4
action=check_login
It works on a normal server, but fails with a 404 when using PhpStorm's integrated server:
HTTP/1.1 404 Not Found
content-type: text/html
content-length: 148
server: PhpStorm 2016.3.2
date: Tue, 10 Jan 2017 14:19:21 GMT
X-Frame-Options: SameOrigin
X-Content-Type-Options: nosniff
x-xss-protection: 1; mode=block
<!doctype html><title>404 Not Found</title><h1 style="text-align: center">404 Not Found</h1><hr/><p style="text-align: center">PhpStorm 2016.3.2</p>
The file is definitely there. What can I try to resolve this issue?

Laravel5 How to get data from HTTP PUT request?

I have HTTP PUT API but cannot retrieve from the request. Input::all() returns empty. Can you share how to do it? I confirmed that $request had form-data.
public function update(Request $request, $id)
{
Log::info(Input::all()); // empty
Log::info($id); // 1001
Log::info($request);
}
$request ->
local.INFO: PUT /v1/shops/1001 HTTP/1.1
.
.
.
------WebKitFormBoundary2B26VoCplFAB8W36
Content-Disposition: form-data; name="item_id"
9001
------WebKitFormBoundary2B26VoCplFAB8W36
Content-Disposition: form-data; name="name"
tokyo
Route.php
<?php
Route::resource('v1/shops', 'ShopController');
?>
Detail
$request ->
local.INFO: PUT /v1/shops/1001 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: ja,en;q=0.8,en-US;q=0.6
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 332
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryAchDYDABHI1GDUeu
Cookie: XXXXXXXXXXXXXX
Host: XXXXXXXXXXXXXX
Origin: chrome-extension://aicmkgpgakddgnaphhhpliifpcfhicfo
Postman-Token: 434976e3-3476-3dcd-3689-7048d523dd20
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
------WebKitFormBoundary2B26VoCplFAB8W36
Content-Disposition: form-data; name="item_id"
9001
------WebKitFormBoundary2B26VoCplFAB8W36
Content-Disposition: form-data; name="name"
tokyo
I found it. I should have selected "x-xxx-form-urlencoded". not "form-data"
I got this form data and could retrieve by Input::all()
user_id=1&address=2&tag=3
This issue a was clue. https://github.com/postmanlabs/postman-app-support/issues/8

Get custom request header

Im running PHP version 5.5 on WAMP. I have a very simple API. I want to get the custom request header called "api_key". First of all, I made the GET request and logged the headers like this:
foreach (getallheaders() as $name => $value) {
$message .= "$name: $value\n";
}
file_put_contents('headers.log', $message);
This resulted in:
Host: localhost
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/javascript, */*; q=0.01
device_id: 63843
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
api_key: hv7Vgd4jsbb
Referer: http://localhost/server/cli/beaufort/www/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: PHPSESSID=bd3c8ce878ebc504b2128686efbe30cf;
bd3c8ce878ebc504b2128686efbe30cf=DEFAULT%7C0%7C2M3TMlgUx3gTlaarYzHIdD28l8q9FTcNubt55%2BUGpAo%3D%7C7456bf61db3500c8bb7b3bc38082a470ce4a2ad3
So "api_key" is there. However, somehow, when I do:
$message = $_SERVER['HTTP_API_KEY'];
I get the error:
Fatal error: Uncaught exception 'ErrorException' with message 'Undefined index: HTTP_API_KEY'
Why can I not get this header??
$headers = getallheaders();
$message = $headers['api_key'];

Jquery POST with same request not work

i have next problem
$("#btnsave").click(function () {
$.post('svld.php', {
'fnd': $('#fnd').attr("value")
}, function (data) {
alert(data);
}, 'json');
}
slvd.php
header('Content-type: application/json');
var_dump(json_encode($_POST));
in request i see next
fnd http://ya.ru
and have great response
string(24) "{"fnd":"http:\/\/ya.ru"}"
but alert(data) not runed
if i do same request on file parse.php in same folder
if($_POST['fnd']){
header('Content-type: application/json');
echo json_encode($cntTags->returnArrayTags());
}
all work perfect
Have idea what it can be ?
header list
Date Thu, 08 Jul 2010 12:16:02 GMT
Server Apache/2.2.14 (Win32) PHP/5.3.0
X-Powered-By PHP/5.3.0
Content-Length 38
Keep-Alive timeout=5, max=99
Connection Keep-Alive
Content-Type application/json
Host localhost
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept application/json, text/javascript, */*
Accept-Language ru,en-us;q=0.7,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset windows-1251,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Content-Type application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://localhost/tz/
Content-Length 22
Cookie spylog_test=1
The issue is that your HTTP request is returning this string:
string(24) "{"fnd":"http:\/\/ya.ru"}"
What happens is that jQuery is trying to parse that but it fails because it's not a valid JSON string.
Try using echo instead of var_dump:
echo json_encode($_POST);
var_dump() outputs other information that you don't need, and that will screw up jQuery's JSON parsing.
Content-Type application/json Заголовки запросапоказать исходный код
looks alien to me. Looks like a character-set failure.
try
header('Content-type: application/json; charset=UTF-8');

Categories