使用库推送器ajax php发送一些参数时遇到问题。
它让我失望:SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
代码PHP和JS:
<script src="https://js.pusher.com/3.0/pusher.min.js"></script>
<?php $this->load->view('scripts_ini'); ?>
<!-- inline scripts related to this page -->
<script type="text/javascript">
var pusher = new Pusher('9999999999999999');
var channel1 = pusher.subscribe('puntaje_channel1');
channel1.bind('msg_puntaje1', function(rpta) {
console.log('+>'+rpta);
$('#sp_puntaje1').html(rpta);
});
$(document).ready(function() {
$('.puntaje1').click(function(e) {
e.preventDefault();
var puntaje1 = parseInt($('#sp_puntaje1').text());
$.ajax({
url: 'push_puntaje1.php',
type: "POST",
dataType: "json",
data : {
message: val_res,
socket_id: pusher.connection.socket_id
}
}).done(function(rpta) {
console.log('->'+rpta);
$('#sp_puntaje1').html(rpta);
}).fail(function( jqXHR, textStatus, errorThrown){
console.log('error, '+errorThrown);
});
});
});
</script>
代码PHP:
<?php
require 'Pusher.php';
$mensaje1 = $_POST['message'];
$pusher1 = PusherInstance::get_pusher();
$pusher1->trigger(
'puntaje_channel1',
'msg_puntaje1',
$mensaje1,
$_POST['socket_id']
);
echo $mensaje1;
?>
错误firefox:
POST http://localhost:8005/sportipd/page/push_puntaje1.php
messaje=3&socket_id=154278.1632118
error, SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
我希望你能解决我的问题。
答案 0 :(得分:0)
您收到json解析错误是因为没有发送override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var indexpath : NSIndexPath = self.tableView.indexPathForSelectedRow!
var destViewController = segue.destinationViewController as! SecondView
let row: Int = indexpath.row
getMore(row)
destViewController.nameString = namesArray[row]
destViewController.companyString = companyArray[row]
destViewController.homeNumber = homeNumber[row]
destViewController.workNumber = workNumber[row]
destViewController.mobileNumber = mobileNumber[row]
destViewController.emailAddress = self.email
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
if let url = NSURL(string: self.largeImageURL) {
if let data = NSData(contentsOfURL: url) {
dispatch_async(dispatch_get_main_queue()) {
destViewController.pic = UIImage(data: data)
}
}
}
}
destViewController.birthday = birthDate[row]
destViewController.address = self.street + self.city + self.state + self.country + self.zip
}
,您正在发送json
。
将此行text/html
更改为:
echo $mensaje1;
您也可以将echo json_encode($mensaje1);
标头设置为json,以确保它在您的PHP代码中如此读取:
Content-Type