无法发布json数据,响应消息是SyntaxError:意外令牌<

时间:2016-03-13 07:37:03

标签: php jquery json

我也没有得到任何控制台错误,我检查网络选项卡我得到200好消息,这意味着数据已发布..请帮助

这是jquery代码



$(document).ready(function() {
    $('#mc_subscribe').submit(function() {
        $('#response').html('form submitting....');
        var form = $(this);
        $.ajax({
            url: $(this).prop('action'),
            type: 'POST',
            dataType: 'json',
            data: form.serialize()
        }).done(function(data){
            if (data.success == true){
                $('#mc_subscribe')[0].reset();
            	$('#response').addClass('alert-success').removeClass('alert-danger').html(data.message);
            	window.setTimeout(function() {
				    window.location.href = data.redirectURL;
				}, 2000);
            } else{
            	$('#response').addClass('alert-danger').removeClass('alert-success').html(data);
            }
            
        }).fail(function( jqXHR, textStatus, data ) {
  alert( "Request failed: " + data );
});;
        return false;
    });
});




这是我的共同编码控制器代码



class Subscribe extends CI_Controller {

	private $data;

	public function index() {
	    if ($this->input->is_ajax_request()){
	        $result = $this->_subscribe();
	        echo json_encode($result);
	        exit;
	    }
	    
	    $this->load->view('header');
	    $this->load->view('pages/subscribe', $this->data);
	    $this->load->view('footer');
	}

	private function _subscribe(){
	    
	    $success = true;
	    $redirectURL = base_url() + '/pricing';

	    if(!$this->input->post('FNAME')){
	        $message =  "No Full Name provided";
	        $success = false;
	    }
	    if( empty($this->input->post('EMAIL')) || !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_POST['EMAIL']) ){
	        $message =  "No Email address provided";
	        $success = false;
	    }
	    if($success && !$this->input->post('MOBILE')){
	        $message =  "No Phone Number provided";
	        $success = false;
	    }
	    if($success && !$this->input->post('DAYTIME')){
	        $message =  "No Day & Time provided";
	        $success = false;
	    }
	    if ($success){
	        
	        $this->load->library('Mcapi', array(
	            'apikey' => $this->config->item('mailchimp_key')
	        ));
	        
	        $api = new Mcapi($this->config->item('mailchimp_key'));
			$list_id = $this->config->item('mailchimp_list_id');
			
			$merge_vars = Array( 
		        'FNAME' => $this->input->post('FNAME'),
		        'MOBILE' => $this->input->post('MOBILE'),
		        'DAYTIME' => $this->input->post('DAYTIME')
		    ); 
		    
			if($api->listSubscribe($list_id, $_POST['EMAIL'], $merge_vars, '') === true) {	
	            //  It worked!
	            $message = 'Success!';
	        } else {
	            $success = false;
	            //  An error ocurred, return error message
	            $message =  'Error: ' . $api->errorMessage;
	        }
	    }
	    return array('message' => $message, 'success' => $success, 'redirectURL' => $redirectURL);

	}

}




这是HTML表单



  <div class="container">
  <div class="col-md-12">
    <form id="mc_subscribe" class="form-horizontal" action="http://localhost/CI-2/Subscribe/" method="post">
      <fieldset>

      <!-- Form Name -->
      <legend>SUBSCRIBE</legend>
      <div class="form-group">
        <label class="col-md-4 control-label" for=""></label>  
        <div class="col-md-4">
          <input id="" name="FNAME" type="text" placeholder="Full Name" class="form-control input-md" required=""> 
        </div>
      </div>
      <div class="form-group">
        <label class="col-md-4 control-label" for=""></label>  
        <div class="col-md-4">
          <input id="" name="EMAIL" type="email" placeholder="Email" class="form-control input-md" required="">
        </div>
      </div>
      <div class="form-group">
        <label class="col-md-4 control-label" for=""></label>  
        <div class="col-md-4">
          <input id="" name="MOBILE" type="text" placeholder="Phone Number" class="form-control input-md" required="">
        </div>
      </div>
      <div class="form-group">
        <label class="col-md-4 control-label" for=""></label>  
        <div class="col-md-4">
          <input id="" name="DAYTIME" type="text" placeholder="Day &amp; Time" class="form-control input-md" required="">
        </div>
      </div>
      <div class="form-group">
        <label class="col-md-4 control-label" for=""></label> 
        <div class="col-md-8">
          <input type="submit" name="submit" class="btn btn-success" value="SUBSCRIBE">
        </div>
      </div>

      </fieldset>
    </form>
    <p id="response" class="alert text-center" align="center"></p>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

更改以下行

return array('message' => $message, 'success' => $success, 'redirectURL' => $redirectURL); 

TO

$data = array();

$data['message'] = $message ; 
$data['success'] = $succcess; 
$data['redirectURL'] = $redirectURL; 

return json_encode($data);

在您的控制器中。

似乎你没有以json格式返回数据,