Javascript 如何使用 jQuery 发出指定 contentType 的 jsonp POST 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3860111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:23:44  来源:igfitidea点击:

How to make a jsonp POST request that specifies contentType with jQuery?

javascriptjqueryajaxhttpjsonp

提问by Marcus

I need to make a jsonp POST request with the content type 'application/json'. I can get the POST request to the server like this:

我需要使用内容类型“application/json”进行 jsonp POST 请求。我可以像这样将 POST 请求发送到服务器:

      jQuery.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: success,
        error: error,
        async: true,
        complete: complete,
        timeout: TIMEOUT,
        scriptCharset: 'UTF-8',
        dataType: 'jsonp',
        jsonp: '_jsonp',
      });

But as soon as I add the line:contentType: "application/json"it starts sending it as an OPTIONS request rather than a POST.

但是一旦我添加了这一行:contentType: "application/json"它就开始将它作为 OPTIONS 请求而不是 POST 发送。

How can I specify the content type and still submit the request as a POST?

如何指定内容类型并仍将请求作为 POST 提交?

回答by SLaks

It is not possible to make a JSONP POST request.

无法发出 JSONP POST 请求。

JSONP works by creating a <script>tag that executes Javascript from a different domain; it is not possible to send a POST request using a <script>tag.

JSONP 的工作原理是创建一个<script>从不同域执行 Javascript的标签;无法使用<script>标签发送 POST 请求。

回答by Pratik Butani

Use jsonin dataTypeand send like this:

使用jsondataType发送这样的:

        $.ajax({
            url: "your url which return json",
            type: "POST",
            crossDomain: true,
            data: data,
            dataType: "json",
            success:function(result){
                alert(JSON.stringify(result));
            },
            error:function(xhr,status,error){
                alert(status);
            }
        });

and put this lines in your server side file:

并将此行放在您的服务器端文件中:

if PHP:

如果PHP

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Max-Age: 1000');

if java:

如果Java:

response.addHeader( "Access-Control-Allow-Origin", "*" ); 
response.addHeader( "Access-Control-Allow-Methods", "POST" ); 
response.addHeader( "Access-Control-Max-Age", "1000" );

回答by Dimitri Kopriwa

There's a (hack) solution I've did it many times, you'll be able to Post with JsonP. (You'll be able to Post Form, bigger than 2000 char than you can use by GET)

有一个(hack)解决方案我已经做过很多次了,你将能够使用 JsonP 发布。(您将能够发布表单,大于 2000 个字符,您可以通过 GET 使用)

Client application Javascript

客户端应用程序 Javascript

$.ajax({
  type: "POST", // you request will be a post request
  data: postData, // javascript object with all my params
  url: COMAPIURL, // my backoffice comunication api url
  dataType: "jsonp", // datatype can be json or jsonp
  success: function(result){
    console.dir(result);
  }
});

JAVA:

爪哇:

response.addHeader( "Access-Control-Allow-Origin", "*" ); // open your api to any client 
response.addHeader( "Access-Control-Allow-Methods", "POST" ); // a allow post
response.addHeader( "Access-Control-Max-Age", "1000" ); // time from request to response before timeout

PHP:

PHP:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Max-Age: 1000');

Doing like this, you are opening your server to any post request, you should re-secure this by providing ident or something else.

这样做,您正在向任何发布请求打开您的服务器,您应该通过提供 ident 或其他东西来重新保护它。

With this method, you could also change the request type from jsonp to json, both work, just set the right response content type

使用这个方法,你也可以将请求类型从 jsonp 改为 json,都可以,只要设置正确的响应内容类型

jsonp

jsonp

response.setContentType( "text/javascript; charset=utf-8" );

json

json

response.setContentType( "application/json; charset=utf-8" );

Please not that you're server will no more respect the SOP (same origin policy), but who cares ?

请注意,您的服务器将不再遵守 SOP(同源政策),但谁在乎呢?