未捕获的TypeError:无法读取null的属性'documentElement',AjAX

时间:2016-07-26 14:35:26

标签: javascript php jquery ajax

我正在制作一个像这样的ajax-jquery函数:

@IBAction func takePhoto(sender: AnyObject) {
        var imageViewBackground: UIImageView!
        self.fullScreenView.hidden = false
        self.recordButton.enabled = false
        self.takephoto.enabled = false
        self.recordButton.hidden = true
        self.takephoto.hidden = true

        session.startRunning()

        // add the AVCaptureVideoPreviewLayer to the view and sets the view in fullscreen
        fullScreenView.frame = view.bounds
        fullScreenView.layer.addSublayer(videoPreviewLayer)

        // add action to fullScreenView
        gestureFullScreenView = UITapGestureRecognizer(target: self, action: #selector(ViewController.takePhoto(_:)))
        self.fullScreenView.addGestureRecognizer(gestureFullScreenView)

        // add action to myView
        gestureView = UITapGestureRecognizer(target: self, action: #selector(ViewController.setFrontpage(_:)))
        self.view.addGestureRecognizer(gestureView)

        if (preview == true) {
            if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
                // code for photo capture goes here...

                stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
                    // process the image data (sampleBuffer) here to get an image file we can put in our view

                    if (sampleBuffer != nil) {
                        let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                        let dataProvider = CGDataProviderCreateWithCFData(imageData)
                        let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault)
                        let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)

                        self.fullScreenView.hidden = true
                        self.fullScreenView.gestureRecognizers?.forEach(self.fullScreenView.removeGestureRecognizer)
                        self.session.stopRunning()

                        // save image to the library
                        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

                        imageViewBackground = UIImageView(frame: CGRectMake(0, 0, self.width, self.height))
                        imageViewBackground.image = image
                        imageViewBackground.tag = self.key

                        self.view.addSubview(imageViewBackground)
                    }
                })
            }
        }
        else {
            preview = true
        }
    }

所以当点击我的按钮{class = change}时,它会发送一个ID到beforeUpdate.php,在这个php文件中我生成一个XML文件,然后发送回$('.change').live('click',function(){ $('html, body').animate({ scrollTop: $("h4").offset().top }, 2000); $('h4').text("Edit Your Video Here :"); $('.drop').text("Drop Your Updated Video Here..."); $('.btn').text("Update"); var videoID = this.id; xhr1 = new XMLHttpRequest(); //initiate request xhr1.open('post','beforeUpdate.php',true);//true for asynchronous xhr1.setRequestHeader('Content-Type',"multipart/form-data"); xhr1.setRequestHeader('vidID',videoID); xhr1.onreadystatechange = handleResponse; xhr1.send(null); }); function handleResponse() { if(xhr1.readyState==4)//processing done { if(xhr1.status == 200)//response okie { var xmlResponse = xhr1.responseXML; root= xmlResponse.documentElement; var id = root.getElementsByTagName("ID"); var name = root.getElementsByTagName("Nama"); console.log("ID From Server : "+id); console.log("Name From Server : "+name); } } } 这是我的beforeUpdate.php文件:

xhr1.onreadystatechange = handleResponse;

从DB获取数据时没有任何问题,我可以获取所有数据并将它们放到与之关联的xml元素中。

然后问题来自<?php require 'databaseConnection.php'; openConnection(); $headers = apache_request_headers(); $ID = $headers['vidID']; $hasil = mysql_query("SELECT * FROM video_management where id=$ID"); $result = mysql_fetch_array($hasil); //$test = "ID = ".$ID."\r\n"."Name = ".$result['Nama']; //fwrite($myfile, $test); //fclose($myfile); //Generating XML FILE header('Content-Type: text/xml'); echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'; echo '<response>'; echo '<ID>'; echo $ID; echo '</ID>'; echo '<Name>'; echo $result['Nama']; echo '</Name>'; echo '<Type>'; echo $result['Type']; echo '</Type>'; echo '<Path>'; echo $result['videoPath']; echo '</Path>'; echo '</response>'; closeConnection(); ?> 据说root= xmlResponse.documentElement;那么我在这里做错了什么...... ??? 非常感谢...! :)

注意: 我的浏览器是chrome

1 个答案:

答案 0 :(得分:1)

我通过将post方法更改为get方法而不是使用这些行来解决它:

if(xhr1.status == 200)//response okie
        {
            var xmlResponse = xhr1.responseXML;
            root= xmlResponse.documentElement;
            var id = root.getElementsByTagName("ID");
            var name = root.getElementsByTagName("Nama");

            console.log("ID From Server : "+id);
            console.log("Name From Server : "+name);
        }

我将其更改为

if(xhr1.status == 200)//response okie
        {
            var xmlResponse = xhr1.responseXML;
            var textResponse = xhr1.responseText;
            console.log("textResponse : "+textResponse);

            //root= xmlResponse.documentElement;
            var id = xmlResponse.getElementsByTagName("ID");
            var name = xmlResponse.getElementsByTagName("Name");

            console.log("ID From Server : "+id[0].childNodes[0].nodeValue);
            console.log("Name From Server : "+name[0].childNodes[0].nodeValue);
        }

参考: http://www.w3schools.com/ajax/ajax_xmlhttprequest_response.asp

谢谢大家......:)