doulan8152
2014-10-21 17:03
浏览 163
已采纳

在POST时404但在GET上有200

I met a problem that when I make a POST request, the server responses with 404 error, while making GET request, it responses with 200.

Here are the methods that I have tried:

 $.ajax({
     type:"POST", 
     url: "script.php",
     data:{
         imgBase64: data
     }
 }).done(function(o) {
     console.log('saved');
 });

And:

var data = new FormData();
data.append("data" , dataURL);
var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
xhr.open( 'post', 'script.php', true );
xhr.send(data);

Further more I have tried to include:

header("HTTP/1.0 200 OK", true);
header('Access-Control-Allow-Origin: *');

in the php script. But none of them works. Anyone has idea? Thanks!

EDIT: Here is the php script. By the way, I am using nodejs...

<?php
// requires php5
header('Access-Control-Allow-Origin: *');
define('UPLOAD_DIR', 'images/');
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujingdai5521 2014-11-19 20:32
    已采纳

    I am new to nodejs and finally I found that I should write a function in server.js to process the get request.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题