将Accept头添加到JQuery AJAX GET(通过JSONP)请求 [英] Add Accept header to JQuery AJAX GET (via JSONP) request

查看:286
本文介绍了将Accept头添加到JQuery AJAX GET(通过JSONP)请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将接受头添加到使用jsonp dataType 的jQuery AJAX GET请求,但由于某种原因,它不起作用。这是我的代码。


transaction_type:34,
transaction_tag:902006933,
authorization_num:ET4653,
金额:15.75
});

$ .ajax({
url:https://api.demo.globalgatewaye4.firstdata.com,
类型:GET,
标题:{
'接受':'application / json'
},
data:e4json,
dataType:jsonp,
success:function(response){
alert('SuccessFly called the e4 gateway api');
}
});

我已经尝试了多件事情,但似乎没有任何工作。我查看了 jQuery 网站上的文档,但我无法找到任何很好的例子。



这就是我的请求标题。我需要接受头为'application / json'。

 接受:* / * 
Accept-Encoding: gzip,deflate,sdch
Accept-Language:en-US,en; q = 0.8
连接:keep-alive
Cookie:_fd_session = d69310c5cd4a02a4700b5ba63f0d0c9b
Host:api.demo。 globalgatewaye4.firstdata.com
Referer:http:// localhost:8080 / smart-two-site / customerinfo.html
User-Agent:Mozilla / 5.0(Macintosh; Intel Mac OS X 10_9_0)AppleWebKit / 537.36(KHTML,像Gecko)Chrome / 33.0.1750.152 Safari / 537.36

任何帮助都很棒。谢谢!

解决方案

不幸的是,无法在JSONP请求上设置标头。 JSONP请求是通过向网站添加< script> 标记完成的,然后浏览器像其他任何脚本一样加载。 请参阅JSONP的解释。


I'm trying to add an accept header to a jQuery AJAX GET request which uses the "jsonp" dataType, but for some reason it's not working. Here's my code thus far.

var e4json =  JSON.stringify( { 
                  "thomas_smith106"           : "Daniel244", 
                  "transaction_type"          : "34",
                  "transaction_tag"           : "902006933",
                  "authorization_num"         : "ET4653",
                  "amount"                    : "15.75"
              } ); 

$.ajax ({
    url: "https://api.demo.globalgatewaye4.firstdata.com",
    type: "GET",
    headers : {
        'accepts' : 'application/json'
    },
    data: e4json,
    dataType: "jsonp",
    success: function  (response) {
        alert('Successfuly called the e4 gateway api');
    }
});

I've tried multiple things but nothing seems to be working. I looked at the the documentation on the jQuery site, but I'm not able to find any good examples.

This is what I get for my request headers. I need the accept header to be 'application/json'.

Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:_fd_session=d69310c5cd4a02a4700b5ba63f0d0c9b
Host:api.demo.globalgatewaye4.firstdata.com
Referer:http://localhost:8080/smart-two-site/customerinfo.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36

Any help would be awesome. Thanks!

解决方案

Unfortunately, it is not possible to set headers on a JSONP request. A JSONP request is done by adding a <script> tag to the website, which the browser then loads like any other script. See this explanation of JSONP.

这篇关于将Accept头添加到JQuery AJAX GET(通过JSONP)请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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