Jquery.ajax PUT数据未被PHP解析

时间:2015-08-25 12:11:33

标签: php jquery ajax slim

我正在使用Slim PHP Framework,并尝试使用Jquery.ajax()以下列方式发送FormData:

var data = new FormData();
data.append('some_name', 'some_data');
data.append('a_file', $('input[name=the_file_form_field]').get(0).files[0]));

$.ajax({
    url: 'the_destination_url',
    data: data,
    processData: false,
    contentType: false,
    type: 'PUT',
    dataType: 'json',

    success: function(data, textStatus, jqXHR) {
        //Processing result here
    },

    error: function(jqXHR, textStatus, errorThrown) {
        //Processing result here
    }
});

但是,我尝试了以下方案:

  • 工作:没有文件的Formdata,添加_METHOD = PUT并设置$ .ajax键入:POST
  • 不起作用(php没有收到PUT数据):没有文件的Formdata,设置$ .ajax键入:PUT
  • 不起作用(方法保持POST):带有文件的Formdata,添加_METHOD = put并设置$ .ajax键入:POST
  • 不起作用(php不接收PUT数据):带有文件的Formdata,设置$ .ajax键入:PUT

我有什么遗漏吗?

应用程序要求它使用PUT请求,因此不可能发出POST请求。

3 个答案:

答案 0 :(得分:1)

尝试将数据作为对象发送。 例如

public class ShootAndCropActivity extends Activity implements OnClickListener {

//keep track of camera capture intent
final int CAMERA_CAPTURE = 1;
//keep track of cropping intent
final int PIC_CROP = 2;
//captured picture uri
private Uri picUri;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //retrieve a reference to the UI button
    Button captureBtn = (Button)findViewById(R.id.capture_btn);
    //handle button clicks
    captureBtn.setOnClickListener(this);
}

/**
 * Click method to handle user pressing button to launch camera
 */
public void onClick(View v) {
    if (v.getId() == R.id.capture_btn) {     
        try {
            //use standard intent to capture an image
            Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //we will handle the returned data in onActivityResult
            startActivityForResult(captureIntent, CAMERA_CAPTURE);
        }
        catch(ActivityNotFoundException anfe){
            //display an error message
            String errorMessage = "Whoops - your device doesn't support capturing images!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}

/**
 * Handle user returning from both capturing and cropping the image
 */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        //user is returning from capturing an image using the camera
        if(requestCode == CAMERA_CAPTURE){
            //get the Uri for the captured image
            picUri = data.getData();
            //carry out the crop operation
            performCrop();
        }
        //user is returning from cropping the image
        else if(requestCode == PIC_CROP){
            //get the returned data
            Bundle extras = data.getExtras();
            //get the cropped bitmap
            Bitmap thePic = extras.getParcelable("data");
            //retrieve a reference to the ImageView
            ImageView picView = (ImageView)findViewById(R.id.picture);
            //display the returned cropped image
            picView.setImageBitmap(thePic);
        }
    }
}

/**
 * Helper method to carry out crop operation
 */
private void performCrop(){
    //take care of exceptions
    try {
        //call the standard crop action intent (the user device may not support it)
        Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
        //indicate image type and Uri
        cropIntent.setDataAndType(picUri, "image/*");
        //set crop properties
        cropIntent.putExtra("crop", "true");
        //indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        //indicate output X and Y
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
        //retrieve data on return
        cropIntent.putExtra("return-data", true);
        //start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, PIC_CROP);  
    }
    //respond to users whose devices do not support the crop action
    catch(ActivityNotFoundException anfe){
        //display an error message
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}
}

您的Ajax请求应该是

data: {'formData' : data},

答案 1 :(得分:1)

我使用发布在https://bugs.php.net/bug.php?id=26004

中的人的评论解决了这个问题

显然,只要超出post_max_size指令,PHP就会以静默方式丢弃所有传入的POST数据。

除了post_max_size之外,检查upload_max_filesize指令也很明智。

答案 2 :(得分:0)

获取PHP中从Ajax JQuery发送的PUT数据的最佳方法是:

使用JavaScript发送数据:

  

其中mydata为{k1:“ val1”,“ k2”:“ val2”,}

 $.ajax({
     url: "data.php",
     contentType : "json",
     data : mydata,
     method:"PUT",
     success: function (data) {
         console.log(data);
     },
     error: function (err) {
         console.log(err);
     },
     complete:function (e) {
         console.log(e);
     }
 });

在data.php文件中获取数据的方法是:

parse_str(file_get_contents("php://input"),$putVars);
var_dump( $putVars ); // input from your form
相关问题