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');
Related
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);
<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?
I am trying to connect to http://google.com/ and receive their home page with the following code:
$sock=stream_socket_client("tcp://google.com:80");
fwrite($sock,"GET / HTTP 1/1\r\n");
fwrite($sock,"Host: google.com\r\n");
fwrite($sock,"User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:26.0) Gecko/20100101 Firefox/26.0\r\n");
fwrite($sock,"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
fwrite($sock,"Accept-Language: en-US,en;q=0.5\r\n");
fwrite($sock,"Connection: close\r\n");
fwrite($sock,"Cache-Control: max-age=0\r\n\r\n");
But this is Google's "HTTP/1.0 400 Bad Request" reply:
HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Content-Length: 925
Date: Mon, 03 Feb 2014 04:06:53 GMT
Server: GFE/2.0
Where am I going wrong?
The first line should be:
fwrite($sock,"GET / HTTP/1.1\r\n");
^ ^
Is is possible to create and send a similar HTTP-request using the POST-method in JavaScript?
POST register.jsp HTTP/1.1
Host: hi/iq
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2) Gecko/20021126
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,
video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1
Accept-Language: en-us, en;q=0.50
Accept-Encoding: gzip, deflate, compress;q=0.9
Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66
Keep-Alive: 300
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------29772313742745
Content-Length: 452
-----------------------------29772313742745
Content-Disposition: form-data; name="name"
J.Doe
-----------------------------29772313742745
Content-Disposition: form-data; name="email"
abuse#spamcop.com
-----------------------------29772313742745
Content-Disposition: form-data; name="file-upload"; filename="test.txt"
Content-Type: text/plain
test data with some high ascii: ¿Como estás?
-----------------------------29772313742745--
The most important things I need are in the last 13 lines (form text data + form file data).
The data (text+file) are to be retrieved in a PHP file serverside.
I have already tried using XMLHttpRequest and Formdata, but I can't get it working properly.
The attempt that makes most sense imo is:
var fd3 = new FormData();
fd3.append('mode', 'end'); // set $_POST['mode']="end"
fd3.append('file', image.jpg); // set $_FILES['file']=image.jpg
var xhr3 = new XMLHttpRequest();
xhr3.open("POST", "pages/upload_arc3d.php", false);
xhr3.setRequestHeader("Content-type", "multipart/form-data");
xhr3.send(fd3);
I've been stuck on this problem for several hours: I'm using PHP and cURL to write a sort of PHP proxy. Almost everything works fine, setting cookies, handling redirects, and submitting forms using POST.
Basically, I'm trying to mirror a remote website with my local proxy. To do so, I redirect every request to http://localhost/resource to http://localhost/proxy.php?url=http://remotesite.com/resource that will fetch the resource on the remote website. The redirect is handled by a 404 error page on .htaccess but I guess that using mod_rewrite would not change things.
I'm testing my proxy on a complex application (the latest version of WordPress) deployed on a remote server. The WordPress login works fine and uses POST. However, I found a page where updating a form does not work, and for which all the POST data is not sent at all to the server.
Here is what I see with wireshark listening on the loopback interface:
POST /proxy/wp-admin/media.php?attachment_id=691&action=edit HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.23) Gecko/20110921 Ubuntu/10.04 (lucid) Firefox/3.6.23
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://localhost/proxy/wp-admin/media.php?attachment_id=691&action=edit
Cookie: [snip]
Cache-Control: max-age=0
Content-Type: application/x-www-form-urlencoded
Content-Length: 501
attachments%5B691%5D%5Bmenu_order%5D=0&attachments%5B691%5D%5Bpost_title%5D=fb&attachments%5B691%5D%5Bimage_alt%5D=&attachments%5B691%5D%5Bpost_excerpt%5D=&attachments%5B691%5D%5Bpost_content%5D=foobar&attachments%5B691%5D%5Burl%5D=http%3A%2F%2Flocalhost%2Fproxy%2Fwp-content%2Fuploads%2F2009%2F04%2Ffb.gif&save=Aggiorna+media&post_id=&attachment_id=691&action=editattachment&_wp_original_http_referer=&_wpnonce=02caf30462&_wp_http_referer=%2Fwp-admin%2Fmedia.php%3Fattachment_id%3D691%26action%3Dedit
HTTP/1.1 200 OK
Date: Wed, 19 Oct 2011 16:18:56 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.3.2-1ubuntu4.10
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 5441
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
[content]
While, what I see if listening on the interface connected to the internet is:
POST /wp-admin/media.php?attachment_id=691&action=edit HTTP/1.1
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.23) Gecko/20110921 Ubuntu/10.04 (lucid) Firefox/3.6.23
Host: www.remotesite.com
Accept: */*
Referer: http://www.remotesite.com/wp-admin/media.php?attachment_id=691&action=edit
Cookie: [snip]
X-Forwarded-For: 127.0.0.1
Content-Length: 0
Content-Type: application/x-www-form-urlencoded
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Date: Wed, 19 Oct 2011 16:25:13 GMT
Server: LiteSpeed
Connection: close
X-Powered-By: PHP/5.2.17
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Last-Modified: Wed, 19 Oct 2011 16:25:13 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
Content-Type: text/html; charset=UTF-8
[content]
As you see my proxy is not transmitting the post data to the remote server.
I expect the problem to be related to the encoding of the POSTDATA, as POST variables in this case are in an array (attachments[691][menu_order]=0; attachments[691][post_content]=foobar and so on...).
I tried several changes as suggested by similar posts but haven't managed to change the behavior of the script at all. All this because apparently the first (local) POST sends the data to localhost, but cURL is unable to fetch the POST data (indeed, file_get_contents("php://input") in the code below reads 0 bytes).
I paste here part of my code hoping somebody can help me:
$ch = curl_init( $url );
$headers = array();
if ( isset($_SERVER['CONTENT_TYPE']) ) {
// commenting this out or changing to multipart/form-data does not change anything
array_push($headers, "Content-Type: ".$_SERVER['CONTENT_TYPE'] );
}
if ( count($headers) > 0 ) {
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
}
$postdata = file_get_contents("php://input"); //this turns out to be empty - and so is $_POST
//REQUEST METHOD: since pages are redirected from a 404 error page, we have to handle
//a redirect, so the real method is specified in REDIRECT_REQUEST_METHOD
if ( isset($_SERVER['REDIRECT_REQUEST_METHOD']) && isset($postdata) ){
if ($_SERVER['REDIRECT_REQUEST_METHOD'] == 'POST'){
curl_setopt( $ch, CURLOPT_POST, true );
}
}
else{
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST' ){
curl_setopt( $ch, CURLOPT_POST, true );
}
}
if ( isset($_SERVER['CONTENT_LENGTH'] ) && $_SERVER['CONTENT_LENGTH'] > 0 ) {
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
}
//set cookies
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookietofwd');
curl_setopt($ch, CURLOPT_COOKIEFILE,'/tmp/cookietofwd');
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$out=curl_exec( $ch );
[...]
Why don't you get your POST data from $_POST? The application/x-www-form-urlencoded header appears to be an AJAX request instead of a regular POST, which I am not sure how it is handled by php://input.
You could so something like:
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($_POST) );
Is there a specific reason why you want this in PHP? Why don't you just use nginx? (http://nginx.org/en/)
It will probably do a better job (and faster).