发布到PHP时的HttpErrorResponse-SyntaxError:JSON中位于位置41的意外令牌a

发布时间:2020-07-07 08:10

我试图将表单中的值发布到php页面中以最终保存在mysql数据库中。当我尝试POST时,我不断收到错误响应:

SyntaxError: Unexpected token a in JSON at position 41
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.onLoad (http://localhost:8100/vendor.js:9017:51)
    at ZoneDelegate.invokeTask (http://localhost:8100/polyfills.js:3505:35)
    at Object.onInvokeTask (http://localhost:8100/vendor.js:75756:33)
    at ZoneDelegate.invokeTask (http://localhost:8100/polyfills.js:3504:40)
    at Zone.runTask (http://localhost:8100/polyfills.js:3273:51)
    at ZoneTask.invokeTask [as invoke] (http://localhost:8100/polyfills.js:3586:38)
    at invokeTask (http://localhost:8100/polyfills.js:4727:18)
    at XMLHttpRequest.globalZoneAwareCallback (http://localhost:8100/polyfills.js:4764:25)

我在离子应用程序中使用的代码是:

        let body = {
            fullname: this.slideOneForm.value['fullName'],
            gender: this.slideOneForm.value['gender'],
            age: this.slideOneForm.value['age'],
            mstatus: this.slideOneForm.value['mstatus'],
            flocation: this.slideOneForm.value['flocation'],
            telno: this.slideOneForm.value['telno'],
            email: this.slideOneForm.value['email'],
            username: this.slideOneForm.value['username'],
            password: this.slideOneForm.value['password'],
            enterprise1: this.selected_enterprises[0],
            enterprise2: this.selected_enterprises[1],
            enterprise3: this.selected_enterprises[2],
            farmstock1: this.slideTwoForm.value['farm_stock'][0],
            farmstock2: this.slideTwoForm.value['farm_stock'][1],
            farmstock3: this.slideTwoForm.value['farm_stock'][2],
            farmharvest1: this.slideTwoForm.value['farm_harvest'][0],
            farmharvest2: this.slideTwoForm.value['farm_harvest'][1],
            farmharvest3: this.slideTwoForm.value['farm_harvest'][2],
            harvestmeasure1: this.slideTwoForm.value['harvest_measure'][0],
            harvestmeasure2: this.slideTwoForm.value['harvest_measure'][1],
            harvestmeasure3: this.slideTwoForm.value['harvest_measure'][2],
            land: this.slideTwoForm.value['land'],
            farmgroup: this.slideTwoForm.value['farmgroup'],
            seeds: this.slideTwoForm.value['seeds'],
            acreagelseason: this.slideTwoForm.value['acreagelseason'],
            acreagetseason: this.slideTwoForm.value['acreagetseason'],
            mmnumber: this.slideThreeForm.value['mmnumber'],
            bank: this.slideThreeForm.value['bank'],
            sales: this.slideThreeForm.value['sales'],
            tillage: this.slideThreeForm.value['tillage'],
            financialservice: this.slideThreeForm.value['financialservice'],
            registeredby: this.officername,
            
         };

         body = JSON.stringify(body);
         
         this.postData(body);

        postData(body){
            //let type = 'application/json; charset=utf-8';
            //let headers = new Headers({ 'Content-Type': type });
            //let options = new RequestOptions({ headers: headers });
            
            const httpOptions = {
              headers: new HttpHeaders({
                'Content-Type':  'application/json',
                'content-type':"application/x-www-form-urlencoded"
                })
              };

            return this.http.post(this.server + "add_farmer.php", body, httpOptions)
              .subscribe(async data => 
              {
                  
                 // 
                  
                 this.dataRegister = data;
                 
                 
                    
               
              });
        }

我在PHP页面中使用的代码是:

            header('Access-Control-Allow-Origin: http://localhost:8100 ');
            header('Access-Control-Allow-Credentials: true');
            header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');
            header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
            header('Content-Type: application/json; charset=UTF-8');

            include 'config.php';

            $postjson = json_decode(file_get_contents('php://input'), true);

            $username = mysqli_query($db_handle, "SELECT * FROM farmers WHERE username='" . $postjson['username'] . "'");

我尝试过类似的变化

body = JSON.parse(JSON.stringify(body));

但是它仍然返回相同类型的错误,但通常是有关“位置0处的意外令牌”的错误。

谢谢

回答1

排序。我的sql请求中出现语法错误:)