jQuery ajax开机自检json [英] jQuery ajax POST json

查看:77
本文介绍了jQuery ajax开机自检json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这是我的问题(我做了研究,尝试通过查看此处发布的其他类似问题来解决我的问题,但没有用)

So, here is my issue ( I did study and try to fix my issue by looking at other similar issue posted here but did not work )

代码段(忽略花括号和其他东西):

Snippet of code (Ignore braces and stuff):

json_string = JSON.stringify(json_links); 
var data_obj = { id:n_id, links_json: json_string }; 

$.ajax({ 
url: 'server_api', 
type: 'GET', 
data: data_obj, 
dataType: 'json',

在服务器端,我尝试使用perl模块,JSON和函数decode_json进行解码,但会引发异常,"JSON字符串中格式错误的UTF-8字符,字符偏移量为48(在"\ x {92f}之前,它-...)''

On server side, I try to decode using perl module, JSON and function, decode_json but it throws an exception, 'malformed UTF-8 character in JSON string, at character offset 48 (before "\x{92f}does-it-...") '

我尝试使用POST方法&内容类型组合,但没有用. jQuery文档指定默认情况下将其编码为UTF-8吗?请帮忙吗?

I tried using POST method & content-type combination but it didn't work. jQuery documentation specify that it encodes to UTF-8 by default? Please help?

推荐答案

您可能正在将已编码的Unicode字符传递给解码器_json,而不是二进制UTF-8字符. 请参阅此处以了解问题和两个简单的潜在修补程序.

You are probably passing encoded Unicode characters to decode_json, not binary UTF-8 characters. See here for the issue and two easy potential fixes.

其他一些事情是您没有在data_obj中转义实体名称(我假定那些实体名称本身就是实体名称,给定它们的关联值).而是尝试插入这些变量的内容.我假设您希望这些本身就是标识符:

Some other things are that you aren't escaping entity names (I assume those are meant to be entity names in and of themselves, given their associated values) in your data_obj. Instead it is trying to insert the contents of those variables. I'm assuming you wanted those to be the identifiers themselves:

data_obj = { 'id' : n_id, 'links_json' : json_string }

除非您只是使用此方法访问已经存储在服务器上的数据,否则应考虑改用POST(或PUT),以便更适当地实现RESTful.

Unless you are simply accessing data already stored on the server using this, you should consider using POST (or PUT) instead, to be more properly RESTful.

此外,GET是通过附加到URL来执行的,您需要注意以下事项:

Also, GET is performed by appending to the URL, and you need to be aware of:

dataObject,字符串

dataObject, String

要发送到服务器的数据.如果还不是字符串,则将其转换为查询字符串.它被附加到GET请求的URL上.请参阅processData选项以防止这种自动处理.

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing.

processData,布尔值

processData, Boolean

默认值:true

默认情况下,作为对象传递给data选项的数据(从技术上讲,不是字符串)将被处理并转换为查询字符串,以适合默认的内容类型"application/x-www-form" -urlencoded".如果要发送DOMDocument或其他未处理的数据,请将此选项设置为false.

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

因为数据附加了URL,即使如果您的JavaScript尝试发送正确的二进制编码的UTF-8,它仍可能会转换为编码的Unicode.

Because the data is URL appended, even if your javascript is trying to send properly binary encoded UTF-8, it may still get translated into encoded Unicode.

此外,datatype与您正在发送 的数据无关,它指的是您希望接收 back 的数据.我以为您意识到了这一点,但我想确定.

Further, datatype is irrelevant for the data you are sending and refers to what data you expect to receive back. I assume you realize this but I wanted to be sure.

因此,您可能想自己对data_obj进行字符串化,而不是让.ajax为您完成.

So you may want to stringify your data_obj yourself rather than let .ajax do it for you.

您是否尝试过简单地回显接收到的(服务器端)原始数据(不运行json解码),以查看接收到的服务器端数据以及它是否符合您的预期?没有样本数据集可以为您复制,很难说出您的问题必定是什么.我假设这将与Perl的json_decode和相关的编码问题(这里的第一行)有关.

Have you tried simply echoing out your received (server side) data raw (without running a json decode) to see what you're receiving server side, and whether it matches what you expected? It's hard to tell what your problem is necessarily without a sample dataset that replicates it for you. I'm assuming it's going to be related to Perl's json_decode and related encoding issues (the first line here), however.

这篇关于jQuery ajax开机自检json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆