如何在jQuery ajax dataType中使用多个值? [英] How to use multiple values with jQuery ajax dataType?

查看:344
本文介绍了如何在jQuery ajax dataType中使用多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该请求数据作为jsonp来执行跨域请求.但是返回的实际结果是{"q":"iphone","r":["iphone 5","iphone","обмен на iphone","iphone 4","iphone 5s"]}之类的json.

I should request the data as jsonp to perform cross domain request. But actual result returned is json like {"q":"iphone","r":["iphone 5","iphone","обмен на iphone","iphone 4","iphone 5s"]}.

我尝试在 dataType 中使用多个值,例如:

I've tried to use multiple values in dataType like:

$.ajax({
  url: url,
  type: 'GET',
  dataType: 'jsonp json',
  jsonp: false,
  ... 

,但它返回parsererror(与jsonp相同).

but it returns parsererror (the same as just with jsonp).

我也尝试使用callback和不使用callback进行通话:

I also tried to do the call with and without callback:

$.ajax({
  url: url,
  type: 'GET',
  dataType: 'jsonp json',
  cache: true,
  jsonpCallback: 'callbackFunctionName',
  jsonp: 'callback',

我该怎么做才能正确处理此类结果?

What can I do to process such result correctly?

已更新.我尝试使用script而不是jsonp,它的效果更好-调用了success/done函数(而不是error/fail),但是我无法获取响应文本-数据传递给success()undefined,并且传递给complete()jqXHR.responseText为空.

Upd. I tried to use script instead of jsonp, it works better - success/done function is called (instead of error/fail), but I can not get response text - data passed to success() is undefined as well as jqXHR.responseText passed to complete() is empty.

推荐答案

您不能使用多个dataType,如果您使用JSONP,这将返回一个jsonp块,您可以使用该块调用回调来处理返回数据,如下所示:

You can't use multiple dataTypes, if you use JSONP this will return a jsonp block which you could use to call a callback to handle the return data like this:

将.ajax()与JSONP结合使用的基本示例吗?

您要返回以jsonp块形式形成的响应,如下所示:

You want to return a response formed as a jsonp block which would be something like:

callback({"q":"iphone","r":["iphone 5","iphone",обменнаiphone","iphone 4","iphone 5s"]}));

callback({"q":"iphone","r":["iphone 5","iphone","обмен на iphone","iphone 4","iphone 5s"]});

从这里开始,您可以假设您正在使用带有set callback参数的ajax调用来使用回调.

From here you can use the callback assuming that you are using the ajax-call with the set callback parameter.

这篇关于如何在jQuery ajax dataType中使用多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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